• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Proper Use of Exception

 
raj malhotra
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
raj malhotra
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what if get NullpointerExceptio say,what you think is the appropriate message to display in error page
 
Amit A. Patil
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48374
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic