This week's book giveaway is in the Mac OS forum. We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line! See this thread for details.
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?
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. hth, bear [ August 06, 2003: Message edited by: Bear Bibeault ]