Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exception handling in servlets

Status
Not open for further replies.

patnim17

Programmer
Jun 19, 2005
111
0
0
US
Hi,
I have a piece of code in the serlvlet like this:

try{
response.sendRedirect("page1.jsp");
}
catch(Exception e)
{
System.out.println(e.getMessage());
try{
response.sendRedirect("error.jsp");
}catch(Exception ex)
{
System.out.println(ex.getMessage());
}

}

if I keep the response.sendRedirect("error.jsp") without the try and catch block I get a compile error saying "Unreported IOException". That is why I put it under a try catch block. But now I want to make sure that this error is not just caught but also bubbled up...
how can I do that?

pat
 
if you want to "bubble up" an exception, re-throw it. just looking at this without testing, gives the feeling it could be an infinite loop?

try {
response.sendRedirect("page1.jsp");
}
catch(Exception e) {
System.out.println(e.getMessage());

try {
response.sendRedirect("error.jsp");
}
catch(Exception ex) {
System.out.println(ex.getMessage());
throw ex;
}
}

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
Your doPost() and doGet() method should throw ServletException and IOException (and any method called by those methods which do any response object handling).

Then you have :

Code:
void doPost(........) throws ServletException, IOException {
.....

  try {
    response.sendRedirect("page1.jsp");
  } catch(Exception e) {
    e.printStackTrace(System.err);
    response.sendRedirect("page1.jsp");
  }

}

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top