• Post Reply Bookmark Topic Watch Topic
  • New Topic

Standard way of handling exception + Java Class + Servlet  RSS feed

 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How should i handle exception in java class and servlet....

well what i did is... created one bollean variable and set its value to true in catch block... and when redirecting...

if the boolean variable is true than redirect to error page else
to successfull page...

but i dont think its the right way of handling exception...and infact i'm not getting the expected results sometimes....

can anyone suggest or give hint about standard way of handling exception ???
 
Ricky Martaputra
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use web.xml setting..

First, if it is a checked exception, catch your exception, and rethrow with ServletException..


If it is a runtime exception, it does not a matter if you don't catch in servlet.

Then, configure your web.xml, just put your exception, in this case MyException or any RuntimeException in an <exception-type> tag and the location of the error pages target.

<error-page>
<exception-type>net.rc.exception.MyException</exception-type>
<location>/error/ErrorPages.html</location>
</error-page>

Put it before you put:
<error-page>
<exception-type>javax.servlet.ServletException</exception-type>
<location>/error/ErrorPages.html</location>
</error-page>
 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if (f.errFlagPost){
resp.sendRedirect("/ui/errors/upr.shtml");
}else{
RequestDispatcher view = getServletContext().getRequestDispatcher("/jsp/feedbackSent.jsp");
view.forward(req,resp);
}

this is how i handled exception...
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jigar Naik:

this is how i handled exception...


What a mess!

As Ricky pointed out, you shouldn't be doing any of that.

Servlets allow ServletException and IOException to be thrown out of their doXXX() methods. The only exception handing you should be doing is to catch other exception types in order to wrap them in a ServletException (as its root cause) and rethrow the exception.

I prefer to be a little more verbose about what went wrong than shown in Ricky's example. Let's assume that a SQLException is thrown while deleting a Thingamambob:



Note that the main reason for the exception and original message are captured as the first parameter, and that the original SQLException is recorded as the root cause via the second parameter.

No booleans. No redirecting or forwarding to error pages in your code.

In your deployment descriptor use the <error-page> element to declare a servlet (or JSP) to handle all errors,

This has numerous advantages. Errors are handled in one place and in a consistent fashion. And you don't have to worry about putting a lot of goop into your servlets and onto your JSPs to handle exceptions.

I prefer to use an error handling servlet that does all the error book-keeping, which then forwards to a user-facing JSP page that let's users know that an error has occured and has been logged.
[ January 19, 2007: Message edited by: Bear Bibeault ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!