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使得编译通过。

原文地址:https://www.cnblogs.com/gujianzhe/p/6750047.html