异常处理一
2023-08-02 本文已影响0人
程序员札记
基本处理流程图
![](https://img.haomeiwen.com/i26273155/3d1d764f3f71553e.png)
通常的异常处理
我们来演示下,看看通常异常是怎么处理的,我们什么都不加,只是这样,看看内部怎么处理的:
![](https://img.haomeiwen.com/i26273155/97f15e227c282cf4.png)
运行:
![](https://img.haomeiwen.com/i26273155/1ae09b4688c07f3d.png)
运行到这里的时候直接抛异常:
![](https://img.haomeiwen.com/i26273155/d900c7af62fa90d6.png)
doDispatch
方法中会保存异常。
![](https://img.haomeiwen.com/i26273155/93d49a6e657fac06.png)
然后处理结果:
![](https://img.haomeiwen.com/i26273155/537aad92b8656c16.png)
走有异常的逻辑:
![](https://img.haomeiwen.com/i26273155/c906fa90c3e1307f.png)
进行异常解析器解析,DefaultErrorAttributes
只是添加了异常属性,什么都没做:
![](https://img.haomeiwen.com/i26273155/cad97fc4ad24accb.png)
而HandlerExceptionResolverComposite
是一个异常解析器的符合解析器,里面有解析器处理。
![](https://img.haomeiwen.com/i26273155/b16f7c16655137cc.png)
是否能解析异常
ExceptionHandlerExceptionResolver的shouldApplyTo
一般只解析HandlerMethod
处理器类型的异常,刚好我们就是这个类型的处理器。
![](https://img.haomeiwen.com/i26273155/38df18baa1f73872.png)
最终到AbstractHandlerExceptionResolver
的shouldApplyTo
,我们没有映射的处理器来处理,所以全部为空的话也是返回true
,表示可以处理:
![](https://img.haomeiwen.com/i26273155/fa2470f3707454e2.png)
doResolveHandlerMethodException处理
如果没有设置异常处理方法就返回null
了,等于没处理,其实这里的方法是可以自定义的,后面说。
![](https://img.haomeiwen.com/i26273155/46795f29e53d3f01.png)
ResponseStatusExceptionResolver的处理
这个也是AbstractHandlerExceptionResolver
的shouldApplyTo
,最终也是没处理:
![](https://img.haomeiwen.com/i26273155/7ed499a872bee279.png)
DefaultHandlerExceptionResolver的处理
这个也是AbstractHandlerExceptionResolver
的shouldApplyTo
,但是最后还是没处理,不过他已经判断了很多中异常了:
@Override
@Nullable
protected ModelAndView doResolveException(
HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex) {
try {
if (ex instanceof HttpRequestMethodNotSupportedException) {
return handleHttpRequestMethodNotSupported(
(HttpRequestMethodNotSupportedException) ex, request, response, handler);
}
else if (ex instanceof HttpMediaTypeNotSupportedException) {
return handleHttpMediaTypeNotSupported(
(HttpMediaTypeNotSupportedException) ex, request, response, handler);
}
else if (ex instanceof HttpMediaTypeNotAcceptableException) {
return handleHttpMediaTypeNotAcceptable(
(HttpMediaTypeNotAcceptableException) ex, request, response, handler);
}
else if (ex instanceof MissingPathVariableException) {
return handleMissingPathVariable(
(MissingPathVariableException) ex, request, response, handler);
}
else if (ex instanceof MissingServletRequestParameterException) {
return handleMissingServletRequestParameter(
(MissingServletRequestParameterException) ex, request, response, handler);
}
else if (ex instanceof ServletRequestBindingException) {
return handleServletRequestBindingException(
(ServletRequestBindingException) ex, request, response, handler);
}
else if (ex instanceof ConversionNotSupportedException) {
return handleConversionNotSupported(
(ConversionNotSupportedException) ex, request, response, handler);
}
else if (ex instanceof TypeMismatchException) {
return handleTypeMismatch(
(TypeMismatchException) ex, request, response, handler);
}
else if (ex instanceof HttpMessageNotReadableException) {
return handleHttpMessageNotReadable(
(HttpMessageNotReadableException) ex, request, response, handler);
}
else if (ex instanceof HttpMessageNotWritableException) {
return handleHttpMessageNotWritable(
(HttpMessageNotWritableException) ex, request, response, handler);
}
else if (ex instanceof MethodArgumentNotValidException) {
return handleMethodArgumentNotValidException(
(MethodArgumentNotValidException) ex, request, response, handler);
}
else if (ex instanceof MissingServletRequestPartException) {
return handleMissingServletRequestPartException(
(MissingServletRequestPartException) ex, request, response, handler);
}
else if (ex instanceof BindException) {
return handleBindException((BindException) ex, request, response, handler);
}
else if (ex instanceof NoHandlerFoundException) {
return handleNoHandlerFoundException(
(NoHandlerFoundException) ex, request, response, handler);
}
else if (ex instanceof AsyncRequestTimeoutException) {
return handleAsyncRequestTimeoutException(
(AsyncRequestTimeoutException) ex, request, response, handler);
}
}
catch (Exception handlerEx) {
if (logger.isWarnEnabled()) {
logger.warn("Failure while trying to resolve exception [" + ex.getClass().getName() + "]", handlerEx);
}
}
return null;
}
默认的异常处理都处理不了我这个异常java.lang.ArithmeticException: / by zero
,然后就抛到tomcat
里去了