Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception Handling

 
Jayashree Mohan
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the following fragment of code where a request dispatcher is used as an error handler. Assuming that InvalidTeaException is a direct subclass of java.lang.Exception, what happens if teahouseexceptionhandler.jsp throws InvalidTeaException?



Answer was :
"ServletException was caught by TeaHouseServlet" is written to the web application's log file.??? How is this possible?
 
Rodrigo Alvarez
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here what the servlet spec says about exception handling in forwarded requests:


SRV.8.5 Error Handling
If the servlet that is the target of a request dispatcher throws a runtime exception or a checked exception of type ServletException or IOException, it should be propagated to the calling servlet. All other exceptions should be wrapped as ServletExceptions and the root cause of the exception set to the original exception, as it should not be propagated.



But I understand that this must be respected by the devlopper (us), not the container.

How come the doXXX() method of the servlet where the request is forwarded throws something else then a ServletException or a RuntimeException? This violates the doXXX() declaration of HttpServlet.
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I understand that this must be respected by the devlopper (us), not the container.

How come the doXXX() method of the servlet where the request is forwarded throws something else then a ServletException or a RuntimeException? This violates the doXXX() declaration of HttpServlet.


With servlets, your first sentence would be true, since we create the code ourselves. But remember this is a JSP we're forwarding to, and the servlet code for JSPs is generated by the container. Therefore, in this case the container automatically wraps the original exception in a ServletException which it then throws. So in fact, no violations of the specification occur.

For future reference, note also that this is bad design practise anyway - catching a known exception in a servlet and choosing to forward to another page is not easily maintainable. The better solution is to configure an <error-page> in the deployment descriptor with the <exception-type> subelement set to the appropriate Throwable subclass you want to catch. This causes the container to display a custom error page for any exceptions of this type not caught by any servlets.
 
Rodrigo Alvarez
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Charles,

But remember this is a JSP we're forwarding to


Ah, yes, I had missed that part.

Once again, your explanations are crystal clear. Thanks a lot for this and all the comments you put on this forum,

Cheers

Simon
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic