You can declare a ServletException in your web.xml but if you want to differentiate between various business exceptions you have to put the business exception type in your web.xml. That way you can define an error page per exception-type. This is what the servlet specification 2.4 says about it:
SRV.9.9.2 Error Pages
The Web application may have declared error pages using the exception-type
element. In this case the container matches the exception type by comparing
the exception thrown with the list of error-page definitions that use the
exception-type element. A match results in the container returning the resource
indicated in the location entry. The closest match in the class heirarchy wins.
If no error-page declaration containing an exception-type fits using the
class-heirarchy match, and the exception thrown is a ServletException or
subclass thereof, the container extracts the wrapped exception, as defined by the
ServletException.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.
posted 10 years ago
Thanks all for your information........I am now able to throw my exception and display in my JSP.
Can anyone explain to me what it is jagan is not doing that he should be doing to handle the exception being raised in the business class? The web.xml looks fine to me in that it maps MyException to an error handler page, and so I do not quite grasp what it is he needed help with. I also don't understand the reason for the suggestion to wrap it in a ServletException.
Costs matter. Justice lies in processes not outcomes. Crime is caused by criminals.
The doXXX methods of HttpServlet only allows ServletException and IOException to be thrown. So in order for other checked exceptions to be propagated outward (as they should be), they are wrapped in a ServletException which is then thrown. That way, the original exception becomes the root cause of the ServletException and its information is not lost.
It's a common newbie mistake to catch exceptions in the servlets and not wrap/throw them again.