
Exception Handling
There are a couple of ways that you can go about exception handling in your JSPs. You could use a traditional
try-catch in your scriptlet.
<%! int[] array = {0,1,2,3,4};%>
<html>
<body>
<%
try{
for(int i = 0; i<= array.length; i++){
//code will break because of “<=” instead of “<”
out.println("array[" + i + "] is: ");
out.println(array[i]);
out.println("<br>");
}
}catch(Exception ex){
out.println("You broke it!");
}
%>
</body>
</html>
The code above contains the classic “off by one” error whereby an ArrayIndexOutOfBoundsException is
thrown the last time through the loop.
Using try-catch mixes too much HTML and Java so it is typically avoided.
Instead of using try-catch, the preferred way to handle exceptions in JSPs is with the use of isErrorPage and
errorPage page directives. The page directive and isErrorPage attribute is used on the page that serves as the
central display for all exceptions. The page directive and errorPage attribute, informs the container where to
direct pages with problems.
JSP error pages are used as the “catch” statement of a try-catch block. One of the benefits of using error
pages is that you can code them for specific exceptions or you can create a basic “catch-all” error page.
<%@ page isErrorPage="true" import="java.io.*"%>
<html>
<body>
Exception encountered: <b><%= exception %></b> <br/>
Exception Message: <b><%= exception.getMessage() %></b> <br/>
Exception location: <br/><b>
<%
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
exception.printStackTrace(pw);
out.println(sw);
%></b>
</body>
</html>
This code demonstrates a generic error page.
Note the page directive stating that this page is an error page. In order for other JSP pages to use this page to
handle exceptions, include the page directive and errorPage attribute directing them to the error page. If the
name of the error page above was exceptionHandler.jsp, then the other pages that use the error page would
include the following page directive:
<%@ page errorPage="exceptionHandler.jsp" %>
This tells the JSP that any exception that is not handled should be sent to the page specified in the
‘errorPage’ attribute.
<%@ page errorPage="exceptionHandler.jsp" %>
<%! int[] array = {0,1,2,3,4};%>
<html>
<body>
<%
for(int i = 0; i<= array.length; i++){
out.println("array[" + i + "] is: ");
out.println(array[i]);
out.println("<br>");
}
%>
</body>
</html>
The result of running this JSP, which would call the error page “exceptionHandler.jsp”, would look something
like the following.
The ‘exception’ Implicit Object
In the previous examples, you may have noticed the use of “exception” to output information about the
details of what problems were encountered. Just like ‘request’, ‘response’, and ‘out’, ‘exception’ is an
implicit object. You don’t need to create a reference because one already exists. As with any exception
object, the three main uses of the ‘exception’ object are demonstrated in the lines of code below.
<%=exception%> //calls the ‘toString()’ method of the exception
<%= exception.getMessage() %> //if a message exists, this will print the
message
<% exception.printStackTrace(); %> //prints a stack trace
When doing a stack trace in JSPs and servlets, it is necessary to use the overloaded printStackTrace() method.
The overloaded printStactTrace() takes a PrintWriter as an argument.
<%
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
exception.printStackTrace(pw);
out.println(sw);
%>
Exceptions
Table of Contents
Copyright (c) 2008. Intertech, Inc. All Rights Reserved. This information is to be used exclusively as an
online learning aid. Any attempts to copy, reproduce, or use for training is strictly prohibited
Courseware
Training Resources
Tutorials
Services