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

北城余情 提交于 2019-11-28 12:49:57
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:

@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:

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

See also:

I have today the same issue. (JavaEE 7 and 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.

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

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.

This will need reflection and policy file could break it.

I hope that this response solves future issues.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!