Sun Java Solaris Communities My SDN Account
 
Tutorials & Code Camps

NO TITLE

 
[Top] [Prev] [Next] [Bottom]

Handling Exceptions

What was happening the last time you used a JSP application and you entered something incorrectly? If the application was well written, it probably threw an exception and displayed an error page. Exceptions that occur while a JSP application is running are called runtime exceptions.

Just as in a Java application, an exception is an object that is an instance of java.lang.Throwable or one of its subclasses. Throwable has two standard subclasses-java.lang.Exception, which describes exceptions, and java.lang.Error, which describes errors.

Errors are different from exceptions. Errors usually indicate linkage or virtual machine problems that your Web application probably won't recover from, such as running out of memory. Exceptions, however, are conditions that can be caught and recovered from. These exceptions might be, for example, a NullPointerException or a ClassCastException, which tell you that a null value or a value of the wrong data type has been passed to your application while it is running.

Runtime exceptions are easy to handle in a JSP application, because they are stored one at a time in the implicit object named exception. You can use the exception object in a special type of JSP page called an error page, where you display the exception's name and class, its stack trace, and an informative message for your user.

A runtime exception is thrown by the compiled JSP file, the Java class file that contains the translated version of your JSP page. This means that your application has already been compiled and translated correctly. (Exceptions that occur while a file is being compiled or translated are not stored in the exception object and have their messages displayed in the command window, rather than in error pages. These are not the type of exception described in this tutorial.)

This tutorial describes how to create a simple JSP application with several display pages, a JavaBeans component, and one error page that gives informative error messages to the user. In this example, the bean tracks which JSP page the user was working in when the exception was thrown, which gives you, the developer, valuable information so that you can display an informative message. This is a simple error tracking mechanism; we will describe more complex ones later in this book.


[Top] [Prev] [Next] [Bottom]

Copyright © 2000, Sun Microsystems, Inc. All rights reserved.

Oracle is reviewing the Sun product roadmap and will provide guidance to customers in accordance with Oracle's standard product communication policies. Any resulting features and timing of release of such features as determined by Oracle's review of roadmaps, are at the sole discretion of Oracle. All product roadmap information, whether communicated by Sun Microsystems or by Oracle, does not represent a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. It is intended for information purposes only, and may not be incorporated into any contract.