web.xml 中的 <error-page> 标记不会捕获 java.lang.Throwable 异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15965869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:28:35  来源:igfitidea点击:

<error-page> tag in web.xml doesn't catch java.lang.Throwable Exceptions

javaservletsweb.xmlcustom-error-pages

提问by andPat

I have a web-app developed with servlet & JSP. I configured my app to throw an IllegalArgumentExceptionif I insert bad parameters. Then I configured my web.xml file in this way:

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

<error-page>
    <error-code>404</error-code>
    <location>/error.jsp</location>
</error-page>
<error-page>
    <exception-type>java.lang.Throwable</exception-type>
    <location>/error.jsp</location>
</error-page>

When I rise a 404 error, then it works and calls error.jsp, but when I rise a java.lang.IllegalArgumentException, then it does not work and I've a blank pageinstead of error.jsp. Why?

当我上升 a 时404 error,它会起作用并调用error.jsp,但是当我上升 a 时java.lang.IllegalArgumentException,它不起作用并且我有一个blank page而不是error.jsp. 为什么?

The server is Glassfish, and logs show really IllegalArgumentException rised.

服务器是Glassfish,日志显示确实出现了IllegalArgumentException。

回答by BalusC

You should not catch and suppress it, but just let it go.

你不应该抓住它并压制它,而只是让它去。

I.e. do not do:

即不做:

@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.
    }
}

But rather just let it go:

而是让它去:

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

Or, if you actually intented to catch it for logging or so (I'd rather use a filter for that, but ala), then rethrow it:

或者,如果您真的打算捕获它以进行日志记录(我宁愿为此使用过滤器,但是 ala),然后重新抛出它:

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

Or, if it's not an runtime exception, then rethrow it wrapped in ServletException, it will be automatically unwrapped by the container:

或者,如果它不是运行时异常,则重新抛出它包裹在 中ServletException,它将被容器自动解开:

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

See also:

也可以看看:

回答by Victor Arribas

I have today the same issue. (JavaEE 7 and Glassfish 4.0)

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

The problem seems that the framework check it as String instead with the Class.

问题似乎是框架将其检查为字符串而不是类。

String based check (the hypothesis)

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

When a Exception is twrown, e.getClass()is compared with <exception-type>as string. So you can't use inheritance.

当抛出异常时,e.getClass()将与<exception-type>字符串进行比较。所以你不能使用继承。

Note that nested classes must be pointed as '$' instead '.' (same as getClass() method).

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

Class based check

基于类的检查

The framework create an instance of the class, and <exception-type>text refer to it, and the class.isInstance()is used to check.

框架创建类的一个实例,<exception-type>文本引用它,class.isInstance()用于检查。

This will need reflection and policy file could break it.

这将需要反射,策略文件可能会破坏它。

I hope that this response solves future issues.

我希望这个回复可以解决未来的问题。