According to Servlet Specification:
A servlet or filter may throw the following exceptions during processing of a request:
- runtime exceptions or errors
- ServletExceptions or subclasses thereof
- IOExceptions or subclasses thereof
If we look at org.springframework.web.servlet.FrameworkServlet#processRequest
, we'll see that Spring throws ServletException
and IOException
, but wraps others including RuntimeExceptions
:
try {
doService(request, response);
} catch (ServletException | IOException ex) {
failureCause = ex;
throw ex;
} catch (Throwable ex) {
failureCause = ex;
throw new NestedServletException("Request processing failed", ex);
}
Why doesn't Spring handle RuntimeException
like IOException
?
UPD: In other words, what wrong would happen if they handle exceptions this way:
try {
doService(request, response);
} catch (ServletException | IOException | RuntimeException ex) {
failureCause = ex;
throw ex;
} catch (Throwable ex) {
failureCause = ex;
throw new NestedServletException("Request processing failed", ex);
}