• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception Handling

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic