File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Proper Use of Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Proper Use of Exception" Watch "Proper Use of Exception" New topic
Author

Proper Use of Exception

raj malhotra
Ranch Hand

Joined: Feb 22, 2007
Posts: 285
Hi,
I am developing one web application,which is connected to oracle database.whenever exception occurs i am rethrowing that exception in application level exception,in catch block.
try{

}catch(Exception e){
e.prinStackTrace();
throw new applicationException("cannot modify employee details");

}

Is it the correct way this is just example .I am catching specific exceptions also.Is it the correct message to display.Even i specify an error page for my application.Is the above message is appropriate .Or i should give some statc message that 'Some Error occured please open new window".What is the correct way to inform user
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Few things regarding your posted code :

Its not a good idea to catch Exception , as this would catch all the fatal runtime exceptions too.

Its good to catch sql exceptions and throw it to the service layer after wrapping it in Application exception.Further the service layer can take care of this exception.You can have custom exception class which might take a code and at the application layer it can be decided as which message would be most appropriate for this exception(By querying the code attached with the application exception).You can have a standard view for displaying the error message to the use according to the code of application exception.


Rahul Bhattacharjee
LinkedIn - Blog
raj malhotra
Ranch Hand

Joined: Feb 22, 2007
Posts: 285
what if get NullpointerExceptio say,what you think is the appropriate message to display in error page
Amit A. Patil
Ranch Hand

Joined: May 04, 2006
Posts: 38
Originally posted by raj malhotra:
what if get NullpointerExceptio say,what you think is the appropriate message to display in error page


Default Exception handler should take care of this case...We use to have this page :-)
"Some Error has occured at the server..Please try again"
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Originally posted by raj malhotra:
what if get NullpointerExceptio say,what you think is the appropriate message to display in error page


FATAL Error occured.Cannot recover.Contact System Administrator.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
Posted by Raj Malhotra
what if get NullpointerExceptio say,what you think is the appropriate message to display in error page
My answer:
Fatal error. Please refer back to whoever programmed this application.

Most NullPointerExceptions (and most RuntimeExceptions) need an alteration to the actual code to sort them out.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
If this message is going out to the end user, you probably want to accomplish one or both of: 1) Let the user know what they should do next and 2) give the help desk some clue what happened when the user calls to complain. I'd prioritize #1. And I'd avoid "fatal" as it has no useful meaning in this context to most non-geeks using a system. Tell them something they can use. "The system was not able to <<verb>> the <<noun>>." Maybe try again, call the help desk, do you want to continue without doing that, etc.

I've often struggled with a chain of throws and catches up the call stack. The lowest level routine usually throws some pretty detailed geek stuff like SQL codes or Null Pointer Exception. The next level up usually catches that, logs it, and throws something with more business meaning. And so on up the call stack. It's not easy to keep the geek stuff away from the user and still not water down the message too much. Faced with these issues, a friend once suggested a single error message for a new system: "Try again, human." It didn't fly.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Proper Use of Exception
 
Similar Threads
Windows 7 Access DB Connection Error "Architecture Mismatch"
Extract all integers from a text file, how to do?
About catching Exception instead of more specific exception.
Exception - Catch
Q: Appropriate Exception Handling in a servlet