Exception Handling in JSP with Examples

FREE Online Courses: Transform Your Career – Enroll for Free!

As we all know that human beings are bound to make mistakes. Same applies to the coding. While coding we might make mistakes thus exception handling is very important. In this article, we will learn about the errors in JSP and Exception handling in JSP with examples.

Exception Handling in JSP

Exception Handling in JSP

Exceptions in JSP can be of three types:

1. Checked Exceptions

Checked exceptions are those exceptions that users make. It also includes exceptions that a programmer and developer can’t see. For example, a file that needs to be compiled can’t be found. The compiler y can’t ignore these exceptions at the time of compilation. Other examples can be an IO Exception or an SQL Exception.

2. Runtime Exceptions

Unlike checked exceptions, the compiler may avoid these exceptions at the compilation time. This exception might have been avoided by the developer or programmer. Arithmetic exception, Null Pointer exception, ArrayIndexOutOfBounds exception fall under this category.

3. Errors

Errors are also ignored at the time of compilation but they are out of the reach of the user or programmer. One cannot do anything about it. These include Instantiation error, Internal error, Stack Overflow etc.

JSP Exception Object

It is an instance of Throwable class and is available just for error pages. Some methods are as follows:

S.No.MethodDescription
1public String getMessage()This message will be initialized in a throwable constructor. This method will provide a description of the exception.
2public Throwable getCause()Gives the cause of the exception.
3public String toString()This method will return the class associated with getMessage().
4public void printStackTrace()Gives the stack trace for the system error with the toString() message.
5public StackTraceElement [] getStackTrace()This method will return the array of all the elements of stack trace.
6public Throwable fillInStackTrace()This method will fill the throwable object with recent stack trace with any previous information.

Catch JSP Exceptions

There are three ways to catch an exception in JSP pages:

1. Using error page and isErrorPage

Syntax:

<%@ page errorPage="exception.jsp" %>
<%@ page isErrorPage="true" %>

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

JSP pages provide the functionality of error page and iserror page implicit objects to deal with errors. This is called page-level exception handling.

For Example:

 main.jsp
<html>
<head>
<title>Enter two Integers to divide</title>
</head>
<body>
<form action="divide.jsp">
Enter First Integer:<input type="text" name="number1" /><br/>
Enter Second Integer:<input type="text" name="mumber2" />
<input type="submit" value="Result"/>
</form>
</body>
</html>
 
divide.jsp
<%@ page errorPage="exception.jsp" %>
<html>
<body>
<%
String num1=request.getParameter("number1");
String num2=request.getParameter("number2");
int n1= Integer.parseInt(num1);
int n2= Integer.parseInt(num2);
int result= n1/n2;
out.print("Output is: "+ result);
%>
</body>
</html>
 
exception.jsp
<%@ page isErrorPage="true" %>
<html>
<head>
<title>Exception</title></head>
<body>
<a href: Hey,Got this Exception: </a><%= exception %> <br/>
<a>Check the data entered.</a>
</body>
</html>

Explanation:
The main page takes two input values to divide. To perform the operation, it calls divide.jsp which specifies that an error occurs then calls exception.jsp. If the user inputs denominator as zero then an exception will occur and exception.jsp runs which specifies that it is an error page. Following are the results:

Output:

Error Page and iserror page in jsp

Exception handling in jsp

2. Try and Catch Block

This method is known for catching exceptions directly using a try and catch block like core java.

Example:

<%@ page language="java" %>
<html>
<body>
<%
try {
int y = Integer.parseInt(request.getParameter("y")) ;
%>
<h3>The number is y=: <%=y%></h3>
<%
} catch (NumberFormatException x) {
%>
<h3>Number Format Exception <br/> Error: y must be a number</h3>
<%
}
%>
</body>
</html>

Explanation:
In this example no. will input through URL query string. If it is a number, it will show the number otherwise it will catch the exception as follows.

Output:

try and catch block in jsp

3. Using web.xml configuration

This method is also known as application-level exception handling. This is done by setting a predefined configuration for certain errors.

For Example:

<error-page>
    <error-code>showerror</error-code>
    <location>showerror.jsp</location>
</error-page>
 
showerror.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
<title>Error</title>
</head>
<body>
<h2> This page could not be found, sorry!!</h2>
 ${pageContext.errorData.requestURI}
</body>
</html>

Explanation: When we execute this code the errorData object will give the information about the error in requestURI of throwable object as follows:

Output:

handle exception in jsp

Conclusion

This article discussed the exception handling in detail. It poured light upon the types of exceptions, methods of Exception. It described three ways to handle exceptions namely: direct handling, page-level exception handling and application level exception handling.

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *