doGet() throws NamingException报错

Posted 孤剑者

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了doGet() throws NamingException报错相关的知识,希望对你有一定的参考价值。

     做一个通过JNDI查找资源的小练习,Context = new InitialContext(),发现NamingException异常,eclipse编辑器只有catch的提示,没有throws的提示,强行在doGet()方法后throws NamingException会报"Exception NamingException is not compatible with throws clause in HttpServlet.doGet(HttpServletRequest, HttpServletResponse)"。

     原因:查看HttpServlet中的doGet方法源码,

根据方法重写的原则,子类重写父类方法时,抛出的异常不能超过父类的范畴,因此在本例中,只能throws ServletException和IOEXception,其他异常必须catch使得编译通过。

 

以上是关于doGet() throws NamingException报错的主要内容,如果未能解决你的问题,请参考以下文章