JSP Exception Handling
When writing JSP programs, programmers may overlook some bugs, which can appear anywhere in the program. JSP code typically has the following kinds of exceptions:
- Checked exception: A checked exception is a typical user error or an error that a programmer cannot foresee. For example, if a file is to be opened but cannot be found, an exception is thrown. Such exceptions cannot simply be ignored at compile time.
- Runtime exception: A runtime exception could have been avoided by the programmer; such exceptions are ignored at compile time.
- Error: An error is not an exception, but the problem is beyond the control of the user or programmer. Errors are usually ignored in code, and there is little you can do about them. For example, a stack overflow error. These errors are all ignored at compile time.
This section will give a few simple and elegant ways to handle runtime exceptions and errors.
Using the Exception Object
The exception object is an instance of a subclass of Throwable and is only available in error pages. The following table lists some important methods in the Throwable class:
| No. | Method**&**Description |
|---|---|
| 1 | public String getMessage() Returns the message of the exception. This message is initialized in the Throwable constructor |
| 2 | public ThrowablegetCause() Returns the cause of the exception, of type Throwable |
| 3 | public String toString() Returns the class name |
| 4 | public void printStackTrace() Prints the exception stack trace to System.err |
| 5 | public StackTraceElement [] getStackTrace() Returns the exception stack trace as an array of stack trace elements |
| 6 | public ThrowablefillInStackTrace() Fills in the Throwable object with the current stack trace |
JSP provides the option to specify an error page for each JSP page. Whenever a page throws an exception, the JSP container automatically invokes the error page.
The following example specifies an error page for main.jsp. Use the<%@page errorPage="XXXXX"%> directive to specify an error page.
<%@ page errorPage="ShowError.jsp" %>
Error Handling Example
<%
// Throw an exception to invoke the error page
int x = 1;
if (x == 1)
{
throw new RuntimeException("Error condition!!!");
}
%>
Now, write the ShowError.jsp file as follows:
<%@ page isErrorPage="true" %>
Show Error Page
Opps...
Sorry, an error occurred.
Here is the exception stack trace:
<% exception.printStackTrace(response.getWriter()); %>
Note that the ShowError.jsp file uses<%@page isErrorPage="true"%>directive, which tells the JSP compiler to generate an exception instance variable.
Now try accessing the main.jsp page, and it will produce the following result:
java.lang.RuntimeException: Error condition!!!
......
Opps...
Sorry, an error occurred.
Here is the exception stack trace:
Using JSTL tags in the error page
You can use JSTL tags to write the error page ShowError.jsp. The code in this example is almost identical in logic to the previous example, but this example has a better structure and can provide more information:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page isErrorPage="true" %>
Show Error Page
Opps...
Error:
${pageContext.exception}
URI:
${pageContext.errorData.requestURI}
Status code:
${pageContext.errorData.statusCode}
Stack trace:
${trace}
The running result is as follows:

Using a try...catch block
If you want to handle exceptions in a single page and handle different exceptions in different ways, you need to use a try...catch block.
The following example shows how to use a try...catch block, placing this code in main.jsp:
Try...Catch Example
<%
try{
int i = 1;
i = i / 0;
out.println("The answer is " + i);
}
catch (Exception e){
out.println("An exception occurred: " + e.getMessage());
}
%>
Try to access main.jsp, and it will produce the following result:
An exception occurred: / by zero