web.xml中的标记不会捕获java.lang.Throwableexception

我有一个用servlet和JSP开发的web应用程序。 如果我插入错误的参数,我将我的应用程序配置为抛出IllegalArgumentException 。 然后我以这种方式配置了我的web.xml文件:

  404 /error.jsp   java.lang.Throwable /error.jsp  

当我出现404 error ,然后它工作并调用error.jsp ,但是当我上升java.lang.IllegalArgumentException ,它不起作用,我有一个blank page而不是error.jsp 。 为什么?

服务器是Glassfish,日志显示IllegalArgumentException上升了。

你不应该抓住并压制它,而是放手吧。

即不要做:

 @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { doSomethingWhichMayThrowException(); } catch (IllegalArgumentException e) { e.printStackTrace(); // Or something else which totally suppresses the exception. } } 

而是放手吧:

 @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doSomethingWhichMayThrowException(); } 

或者,如果你真的想要捕获它用于记录等(我宁愿使用filter,但是ala),然后重新抛出它:

 @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { doSomethingWhichMayThrowException(); } catch (IllegalArgumentException e) { e.printStackTrace(); throw e; } } 

或者,如果它不是运行时exception,那么重新抛出它包装在ServletException ,它将被容器自动解包:

 @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { doSomethingWhichMayThrowException(); } catch (NotARuntimeException e) { throw new ServletException(e); } } 

也可以看看:

我今天有同样的问题。 (JavaEE 7和Glassfish 4.0)

问题似乎是框架将其检查为String而不是Class。

基于字符串的检查(假设)

当一个exception被抛出时, e.getClass()为字符串。 所以你不能使用inheritance。

请注意,嵌套类必须指向“$”而不是“。” (与getClass()方法相同)。

基于class级的检查

框架创建类的实例, 文本引用它, class.isInstance()用于检查。

这需要反思,政策文件可能会破坏它。

我希望这一回应能够解决未来的问题。