错误处理程序Servlet:如何获取异常原因 [英] Error Handler Servlet: how to get exception cause
问题描述
< error-page>
< exception-type> java.lang.Exception< / exception-type>
< location> / ExceptionHandler< / location>
< / error-page>
对?
在我的)servlet:
doGet(HttpServletRequest请求,HttpServletResponse响应)throws ServletException,IOException {
try {
...
...
} catch(Exception e){
throw new ServletException(some mesage,e);
}
}
所以,e将成为这个案例。
在我的ExceptionHandler类中,我有:
doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException {
Throwable throwable =(Throwable)request.getAttribute(javax.servlet.error.exception);
throwable.getCause()// NULL
}
这是问题。 throwable.getCause()为null。
如果servletcontainer捕获的异常是一个 ServletException
并且< error-page>
被声明为捕获除 ServletException
,那么它的原因实际上将被解开并存储为javax.servlet.error.exception
。所以你基本上已经有了 throwable
变量,你不需要调用 getCause()
/ p>
如果没有
错误页
声明包含一个异常类型
适合使用
类层次匹配,抛出的异常是一个ServletException
或
子类,容器提取包装的异常,由
ServletException.getRootCause
方法定义。第二次通过错误
页面声明,再次尝试匹配错误页面声明,
,但使用包装的异常。
顺便说一下,最好使用 RequestDispatcher#ERROR_EXCEPTION
常量,而不是硬编码。
Throwable throwable =(Throwable)request.getAttribute(RequestDispatcher.ERROR_EXCEPTION);
I have an error servlet configured in my web.xml:
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/ExceptionHandler</location>
</error-page>
right?
In my (generically) servlet:
doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
...
...
} catch (Exception e) {
throw new ServletException("some mesage", e);
}
}
so, "e" will be the root cause in this case.
In my ExceptionHandler class, I have:
doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Throwable throwable = (Throwable) request.getAttribute("javax.servlet.error.exception");
throwable.getCause() //NULL
}
this is the problem. throwable.getCause() is null.
If the exception caught by the servletcontainer is a ServletException
and the <error-page>
is declared to catch an exception other than ServletException
, then its cause will actually be unwrapped and stored as "javax.servlet.error.exception"
. So you basically already have it as throwable
variable and you don't need to call getCause()
on it.
See also 5th paragraph of chapter 9.9.2 of Servlet 2.5 specification:
If no
error-page
declaration containing anexception-type
fits using the class-hierarchy match, and the exception thrown is aServletException
or subclass thereof, the container extracts the wrapped exception, as defined by theServletException.getRootCause
method. A second pass is made over the error page declarations, again attempting the match against the error page declarations, but using the wrapped exception instead.
By the way, it's better to use the RequestDispatcher#ERROR_EXCEPTION
constant instead of hardcoding it.
Throwable throwable = (Throwable) request.getAttribute(RequestDispatcher.ERROR_EXCEPTION);
这篇关于错误处理程序Servlet:如何获取异常原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!