aspose file tools*
The moose likes Servlets and the fly likes basic question about exception handling in a web application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "basic question about exception handling in a web application" Watch "basic question about exception handling in a web application" New topic
Author

basic question about exception handling in a web application

Faisal Khan
Ranch Hand

Joined: Jun 29, 2003
Posts: 285
I am writing a new application and have stumbled on a basic issue. I am writing a Database class which will just connect to the database and perform a few other abstract database tasks.
I am not sure what is the best way to record the exceptions in my catch clauses:
1. System.out.println() is a bad candidate since I don't know how the system may be configured in future.
2. I can not use the database as at this stage the database is not even connected to (plus not sure if thats a good technique ever).
3. Should I use something like log4j and log to a text file.
4. Should I pass the buck and eventually let the controller Servlet know there was a connection error and deal with it that way.
I would appreciate help on this matter.
- FK -


The secret to creativity is knowing how to hide your sources.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

3) I like log4j, and I like how you can configure it to ignore messages of certain severity and from certain classes. So it might be worthwhile to log (as debug) the jdbc trouble from the database class.

Then...

4) This is certainly a popular pattern. But instead of exposing a SQLException, you'd instead wrap it in your own 'application/business' exception.
Faisal Khan
Ranch Hand

Joined: Jun 29, 2003
Posts: 285
Mike,
Thank you very much for your feedback and clarification on point 4, I didn't really think of going the route of writing my own exception classes.
- FK -
Scott Duncan
Ranch Hand

Joined: Nov 01, 2002
Posts: 363
I like a mixture of 3 and 4 as well. Throw the exception and catch it in your servlet in doWhatever method:
String forwardPage = "success.jsp";
try{
....
//perform db ops here
....
}
catch(Exception e){
if(e instanceof SQLException){
//log it using log4j
//redirect to error page
forwardPage = "dbUpdate.jsp";
}
else{
//log it using log4j
forwardPage = "500.html";
}
}
response.sendRedirect(forwardPage);


No more rhymes! I mean it!<br /> <br />Does anybody want a peanut?
Faisal Khan
Ranch Hand

Joined: Jun 29, 2003
Posts: 285
Thanks for the affirmation.
brams mun
Greenhorn

Joined: Nov 27, 2003
Posts: 25
yeah,
guys, u got the best approach in my view,
v r also using log4j along with the exception catching.
v r putting the debugging messages in a log file and at the same time using exception, it really released pain out of our heads.
take care.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: basic question about exception handling in a web application