The Wayback Machine - https://web.archive.org/web/20240717024346/https://www.geeksforgeeks.org/exception-handling-in-jsp/
Open In App

Exception handling in JSP

Last Updated : 28 Sep, 2021
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

Java Server Pages declares 9 implicit objects, the exception object being one of them. It is an object of java.lang.Throwable class, and is used to print exceptions. However, it can only be used in error pages.

There are two ways of handling exceptions in JSP. They are: 

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

Handling Exception using page directive attributes

The page directive in JSP provides two attributes to be used in exception handling. They’re:  

  • errorPage: Used to site which page to be displayed when exception occurred.
    Syntax :
 <%@page errorPage="url of the error page"%>
  • isErrorPage: Used to mark a page as an error page where exceptions are displayed. 
    Syntax :
 <%@page isErrorPage="true"%>

In order to handle exceptions using the aforementioned page directives, it is important to have a jsp page to execute the normal code, which is prone to exceptions. Also, a separate error page is to be created, which will display the exception. In case the exception occurs on the page with the exception prone code, the control will be navigated to the error page which will display the exception.

The following is an example illustrating exception handling using page directives:

index.html  

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 

Java




// JSP code to divide two numbers
<% @page errorPage = "error.jsp" %> < %
 
                                          String num1
    = request.getParameter("first");
String num2 = request.getParameter("second");
 
// extracting numbers from request
int x = Integer.parseInt(num1);
int y = Integer.parseInt(num2);
int z = x / y; // dividing the numbers
out.print("division of numbers is: " + z); // result
 
% >


error.jsp 

Java




// JSP code for error page, which displays the exception
<% @page isErrorPage = "true" %>
 
    <h1> Exception caught</ h1>
 
        The exception is : <%= exception %> // displaying the exception


Output: 
index.html 

error.jsp 

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

This is another way of specifying the error page for each element, but instead of using the errorPage directive, the error page for each page can be specified in the web.xml file, using the <error-page> element. The syntax is as follows:

HTML




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


The following example illustrates using this technique to handle exceptions:

index.html 

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 

Java




// 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 

Java




// 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 

HTML




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


The output, in this case, is similar as in the previous one.
 



Similar Reads

Difference between Exception::getMessage and Exception::getLine
Exception::getMessage: The getMessage exception in PHP language is basically used by the programmers to know the Exception message. It means that whenever an exception condition occurs in a code, in order to know the exact meaning and what that exception is all about this function is been used. This function is quite useful for the programmer becau
2 min read
Difference between runtime exception and compile time exception in PHP
The term PHP is an acronym for Hypertext Preprocessor, which is a server-side scripting language designed specifically for web development. It is open-source which means it is free to download and use. It is very simple to learn and use. The files have the extension “.php”. It is an interpreted language and it does not require a compiler. PHP code
3 min read
Comparison of Exception Handling in C++ and Java
Both languages use to try, catch and throw keywords for exception handling, and their meaning is also the same in both languages. Following are the differences between Java and C++ exception handling: Java C++ Only throwable objects can be thrown as exceptions.All types can be thrown as exceptions.We can catch Exception objects to catch all kinds o
4 min read
Exception Handling with Method Overriding in Java
An Exception is an unwanted or unexpected event, which occurs during the execution of a program i.e at run-time, that disrupts the normal flow of the program’s instructions. Exception handling is used to handle runtime errors. It helps to maintain the normal flow of the program. In any object-oriented programming language, Overriding is a feature t
6 min read
Exception Handling in Node.js
Exception handling refers to the mechanism by which the exceptions occurring in a code while an application is running is handled. Node.js supports several mechanisms for propagating and handling errors. There are different methods that can be used for exception handling in Node.js: Exception handling in synchronous code: If an error occurs in a sy
3 min read
Java - Exception Handling With Constructors in Inheritance
Java provides a mechanism to handle exceptions. To learn about exception handling, you can refer to exceptions in java. In this article, we discuss exception handling with constructors when inheritance is involved. In Java, if the constructor of the parent class throws any checked exception, then the child class constructor can throw the same excep
7 min read
Exception Handling in Apache Kafka
Exception handling is an important aspect of any software system, and Apache Kafka is no exception. In this article, we will discuss the various types of exceptions that can occur in a Kafka system and how to handle them. First, it is important to understand the basic architecture of Kafka. A Kafka system consists of a number of brokers, which are
6 min read
Define Exception handling in ES6
Exception: Exceptions are unwanted event that basically interrupts the normal flow of program. There are two types of exception in general:- Synchronous (eg. runtime errors)Asynchronous (eg. system errors) Exception Handling: Often a times exception happens in a program that causes the program to terminate abruptly or in an unfriendly manner. To ha
3 min read
How to do exception handling in Ajax ?
The purpose of this article is to demonstrate how we handle the exception in the jQuery AJAX request. A basic understanding of HTML, CSS, and jQuery is required. This can be done by an AJAX fail() method. We discuss 3 AJAX methods to better understand what's going on when making any ajax() request from our web browser to a particular server. AJAX:
3 min read
Servlet - Exception Handling
When a servlet throws an exception, the web container looks for a match with the thrown exception type in web.xml configurations that employ the exception-type element. To define the invocation of servlets in response to particular errors or HTTP status codes, you'd have to utilize the error-page element in web.xml. Exception Handling is the proces
4 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg