Exception Handling in JSP | What is Exception & Error? | Methods of Handling Exceptions in JSP with Examples

In the previous tutorial, we have discussed JSP Directives. Now, we have come with the new JSP Topic ie., Exception Handling in JSP. So, in this tutorial, we will learn JSP Exception Handling with Examples. Before understanding JSP Exception Handling, let’s start learning what is Exception and how it is different from errors. Just use the direct links for quick reference about the JSP Exception Types, Methods, etc.

What is an Exception?

An exception is an event, that occurs during the execution of a program, that disrupts the normal flow of program instructions. when an error occurs within a method, the methods create objects and hands it off to the runtime system.

The object called an exception object. It contains the information about the error, including its type and state of the program when the error occurred. Exception Handling is the process of handle the errors that may occur during runtime.

Types of Exceptions in JSP

There are three types of JSP Exceptions. They are as follows:

Checked Exceptions

It is usually a user error or problems which are not recognized by the developer are named as checked exceptions.

Runtime Exceptions

Runtime exceptions are the one which could have evaded by the programmer. They are overlooked at the time of compilation.

Error Exception

It is an example of the throwable class, and it is used in error pages.

A few of the throwable class methods are:

  • Public String getMessage() – returns the message of the exception.
  • Public printStackTrace()- returns the stacktrace of the exception.
  • Public throwablegetCause() – returns cause of the exception.

What is Error?

Most of the time errors are not caused by our program these are caused by system resources. We cannot handle the errors.

For instance: if OutOfMemory occurs being a programmer we can’t do anything and the program will be terminated automatically.

Also Read:

Methods of Handling Exception in JSP

In JSP, We can handle the exceptions in two ways. They are as such:

  • By errorPage and isErrorPage attributes of page directive
  • By <error-page> element in web.xml file

Handling Exception using page directive attributes

In JSP Page Directive, there are two attributes that help in handling exceptions in JSP. They are:

1. errorPage:

This attribute is utilized to notify the container to jump the control to another JSP page if an exception occurs in the current JSP page. Extra JSP page is used to handle the exception that is happened in the current JSP page.

Syntax: <%@ errorPage = “error.jsp” %>

2. isErrorPage:

This attribute is utilized to notify the container that makes a JSP an error page or not. The default value of this attribute is false. It means a JSP page is not acting as an error page by default. To make a JSP page as an error page than we should write isErrorPage = “true”. If isErrorPage = “true” then only execution objects is passed into a JSP page.

Syntax: <%@ isErrorPage=”true” %>

Example of exception handling in JSP by the elements of page directive:

In this example, we are creating the 3 files.

  1. index.jsp: for input values.
  2. calculation.jsp for dividing the two numbers and display the result.
  3. error.jsp for handle the jsp exception.

index.jsp

Exception Handling in JSP 1

calculation.jsp

Exception Handling in JSP 2

error.jsp

Exception Handling in JSP 3

Output:

Exception Handling in JSP 4

Exception Handling in JSP 5

Handling Exceptions Using error-page Element in web.xml File

The other way of defining the error page for each element, but in place of using the errorPage directive, the error page for each page can be specified in the web.xml file, with the help of the <error-page> element.

The syntax for <error-page> is as follows:

<web-app> 

<error-page> 
<exception-type>Type of Exception</exception-type> 
<location>Error page url</location> 
</error-page> 

</web-app>

Example of JSP exception handling by the error-page element in web.xml file:

The below example shows the usage of this technique to handle exceptions:

index.html

<html>
<head>
<body>
<form action="a.jsp"> 
Number1:<input type="text" name="first" >
Number2:<input type="text" name="second" > 
<input type="submit" value="divide"> 
</form> 
</body>
</html>

a.jsp

// JSP code to divide two numbers
< %

String num1
= request.getParameter("first");
String num2 = request.getParameter("second");
// extracting the numbers
int x = Integer.parseInt(num1);
int y = Integer.parseInt(num2);
int z = x / y; // dividing
out.print("division of numbers is: " + z); // result

% >

error.jsp

// JSP code for error page, which displays the exception
<%@ page isErrorPage="true" %> 

<h1>Exception caught</h1> 

// displaying the exception
The exception is: <%= exception %>

web.xml

<web-app> 

<error-page> 
<exception-type>java.lang.Exception</exception-type> 
<location>/error.jsp</location> 
</error-page> 

</web-app>

Output:

In this case, the output is as same as the previous one.