While looking through the Spring MVC framework I noticed that, unless I misunderstand, its developers favor throws Exception instead of throwing multiple exceptions.
I realize that at the core of this question is the checked versus unchecked exceptions debate, avoiding that religious war, is it a good practice to use throws generic exception?
What makes sense for a library such as Spring MVC, which needs to be open enough to fit all sorts of different use cases, does not necessarily make sense for you to follow when writing a specific application. This is one of those cases.
If you are referring to classes such as the
Controllerinterface which as a method signature such asThis is likely because, from the perspective of the Spring classes which call into your Controller (such as
DispatcherServlet), they don’t care what type of Exception your code calls – library code such asDispatcherServletonly needs to know that this class may throw an Exception and therefore is capable of handling Exception in the general case.In other words,
DispatcherServletdoesn’t need to know what specific type(s) of Exception your Controller may throw – it’s going to treat any of them as an “error”. This is why the method signature isthrows Exception.Now, the API authors could have made the signature use a custom exception type as
SpringMvcException, but that would only have the effect of forcing you to handle any checked exception types in yourhandleRequestmethod and simply wrap them, which is tedious make-work boilerplate code. So, since pretty much everything with Spring is designed to make it as easy and lightweight for you to integrate with as possible, it’s easier for them to specify that the interface method merelythrows Exception.