File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes User and Developer friendly Exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "User and Developer friendly Exceptions" Watch "User and Developer friendly Exceptions" New topic

User and Developer friendly Exceptions

thomas davis
Ranch Hand

Joined: Feb 01, 2003
Posts: 207
How can we create user defined exception class in order to use it our application? Suppose, I am using Oracle as a database, how can we give user-friendly message to user and developer from the standard exception throwing from the JSP/Servlets?
For example if user forget to enter user name, and he got a NullPointerException? Instead showing NullPointerException, I want to give messages like this “user not entered the user name”?
In another case if user is doing some insertion of data against database, He got some SQLException, if I wanted to give a user-friendly message to user, how can I?
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63865

Exceptions aren't really meant to be user-facing or user-friendly. They're more to deliver information to the developer on what when wrong and why. A stack trace would never be described as a happy event.
You should never throw an exception because of user input error. Exceptions should be thrown only when something 'exceptional', that is, unexpected by the program, occurs.
In order to be able to deliver as much error info as possible to the developer, ServletException and JspException are 'wrapper' exceptions that can be used to wrap a 'root cause' exception. That way, you can throw a ServletException out of your servlet without losing the info in some original exception.
For example, here's an example of how I'd handle a SQL exception from the database:

Note that the original SQL exeption is passed to the ServletException constructor to serve as the root cause of the exception. If I only copied the message of the SQL exception into the message of the ServletException, all the stack trace and other info in the SQL exception would have been lost.
[ August 06, 2003: Message edited by: Bear Bibeault ]

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: User and Developer friendly Exceptions
It's not a secret anymore!