JSP exception handling

When writing JSP application, programmers may miss some BUG, ​​the BUG may appear anywhere in the program. JSP code is generally the following categories exception:

  • Checked exceptions: checked exceptions is a typical user error or a programmer unforeseen errors. For example, if a file is to be opened, but can not find the file, then an exception is thrown. These anomalies can no longer compile is simply ignored.
  • Runtime exception: a runtime exception programmers may have been avoided, this anomaly will be ignored at compile time.
  • Error: There is no exception, but the problem is that it is beyond the control of the user or programmer. Error is often overlooked in the code, you can hardly take it how. By way of example, or stack overflow error. These errors will be ignored at compile time.

This section will give a few simple and elegant way to handle runtime exceptions and errors.


Use Exception Objects

exception object is an instance of Throwable subclass, available only in the error page. The following table lists some of the Throwable class in important ways:

No. Method & description
1 public String getMessage ()

Returns the exception. This information is initialized in the constructor Throwable
2 public ThrowablegetCause ()

Returns the cause of the exception, the object of type Throwable
3 public String toString ()

Returns the name of the class
4 public void printStackTrace ()

The exception stack trace output to System.err
5 public StackTraceElement [] getStackTrace ()

In the form of a stack trace element of the array returns an exception stack trace
6 public ThrowablefillInStackTrace ()

The current stack trace fill Throwable object

JSP provides the option to specify error pages for each JSP page. Whenever the page throws an exception, JSP container will automatically call the error page.

The following examples main specifies an error page. Use <% @ page errorPage = "XXXXX"%> directive specifies an error page.

<%@ page errorPage="ShowError" %>

<html>
<head>
   <title>Error Handling Example</title>
</head>
<body>
<%
   // Throw an exception to invoke the error page
   int x = 1;
   if (x == 1)
   {
      throw new RuntimeException("Error condition!!!");
   }
%>
</body>
</html>

Now, write ShowError following documents:

<%@ page isErrorPage="true" %>
<html>
<head>
<title>Show Error Page</title>
</head>
<body>
<h1>Opps/en.</h1>
<p>Sorry, an error occurred.</p>
<p>Here is the exception stack trace: </p>
<pre>
<% exception.printStackTrace(response.getWriter()); %>

Noting, ShowError file uses <% @ page isErrorPage = "true"%> directive, which tells the JSP compiler needs to generate an exception instance variable.

Now try to access main page, it will produce the following results:

java.lang.RuntimeException: Error condition!!!
/en/en/en

Opps/en.
Sorry, an error occurred.

Here is the exception stack trace:

Use JSTL tags in error page

JSTL tags can be used to write error page ShowError. This example code and logic in the example code is almost the same, but in this case the code has a better structure, and can provide more information:

<%@ taglib prefix="c" uri="https://www.oracle.com/java/technologies/" %>
<%@page isErrorPage="true" %>
<html>
<head>
<title>Show Error Page</title>
</head>
<body>
<h1>Opps/en.</h1>
<table width="100%" border="1">
<tr valign="top">
<td width="40%"><b>Error:</b></td>
<td>${pageContext.exception}</td>
</tr>
<tr valign="top">
<td><b>URI:</b></td>
<td>${pageContext.errorData.requestURI}</td>
</tr>
<tr valign="top">
<td><b>Status code:</b></td>
<td>${pageContext.errorData.statusCode}</td>
</tr>
<tr valign="top">
<td><b>Stack trace:</b></td>
<td>
<c:forEach var="trace" 
         items="${pageContext.exception.stackTrace}">
<p>${trace}</p>
</c:forEach>
</td>
</tr>
</table>
</body>
</html>

Results are as follows:


Use try /en. catch block

If you want to put a page exception handling, and for different exceptions are handled differently, then you need to use a try /en. catch block.

This next example shows how to use the try /en. catch block, the code will be placed in main:

<html>
<head>
   <title>Try/en.Catch Example</title>
</head>
<body>
<%
   try{
      int i = 1;
      i = i / 0;
      out.println("The answer is " + i);
   }
   catch (Exception e){
      out.println("An exception occurred: " + e.getMessage());
   }
%>
</body>
</html>

Try to visit main, it will produce the following results:

An exception occurred: / by zero