Exception Handling in JSP

โšก Smart Summary

Exception Handling in JSP manages errors that occur during code execution, similar to Java try-catch blocks. Exceptions fall into three types: checked, runtime, and error. JSP pages use the errorPage and isErrorPage attributes to redirect and display exceptions.

  • โš ๏ธ Exception Defined: An error during JSP execution, handled like Java try-catch.
  • ๐Ÿ—‚๏ธ Three Types: Checked, runtime, and error exceptions.
  • ๐Ÿงพ Checked: FileNotFoundException, IOException, and SQLException must be handled.
  • ๐Ÿงจ Runtime: ArrayIndexOutOfBounds, ArithmeticException, and NullPointerException.
  • ๐Ÿ›Ÿ errorPage / isErrorPage: Redirect errors to a page that prints the exception stack trace.

Exception Handling in JSP

What is Exception in JSP?

Exceptions in JSP occur when there is an error in the code either by the developer or internal error from the system. Exception handling in JSP is the same as in Java where we manage exceptions using Try Catch blocks. Unlike Java, there are exceptions in JSP also when there is no error in the code.

Types of Exceptions in JSP

Exceptions in JSP are of three types:

  1. Checked Exception
  2. Runtime Exception
  3. Error Exception

Checked Exceptions

It is normally a user error or problems which are not seen by the developer are termed as checked exceptions. Some Checked exception examples are:

  1. FileNotFoundException: This is a checked exception (where it tries to find a file when the file is not found on the disk).
  2. IO Exception: This is also checked exception if there is any exception occurred during reading or writing of a file then the IO exception is raised.
  3. SQLException: This is also a checked exception when the file is connected with SQL database, and there is issue with the connectivity of the SQL database then SQLException is raised

Runtime Exceptions

Runtime exceptions are the one which could have avoided by the programmer. They are ignored at the time of compilation. Some Runtime exception examples are:

  1. ArrayIndexOutOfBoundsException: This is a runtime exception when array size exceeds the elements.
  2. ArithmeticException: This is also a runtime exception when there are any mathematical operations, which are not permitted under normal conditions, for example, dividing a number by 0 will give an exception.
  3. NullPointer Exception: This is also a runtime exception which is raised when a variable or an object is null when we try to access the same. This is a very common exception.

Errors:

The problem arises due to the control of the user or programmer. If stack overflows, then error can occur. Some examples of the error are listed below:

  1. Error: This error is a subclass of throwable which indicates serious problems that an application cannot catch.
  2. Instantiation Error: This error occurs when we try to instantiate an object, and it fails to do that.
  3. Internal Error: This error occurs when there is an error occurred from JVM i.e. Java Virtual Machine.

Error Exceptions

It is an instance of the throwable class, and it is used in error pages. Some methods of throwable class are:

  • Public String getMessage() โ€“ returns the message of the exception.
  • Public throwable getCause() โ€“ returns cause of the exception
  • Public printStackTrace() – returns the stacktrace of the exception.

How to Handle Exception in JSP

Here is an example of how to handle exception in JSP:

Exception_example.jsp

<%@ page errorPage="guru_error.jsp" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Exception Guru JSP1</title>
</head>
<body>
<%
 int num = 10;
   if (num == 10)
   {
      throw new RuntimeException("Error condition!!!");
   }
 %>
   </body>
</html>

Guru_error.jsp

<%@ page isErrorPage="true" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Guru Exception Page</title>
</head>
<body>
<p>Guru Exception has occurred</p>
<% exception.printStackTrace(response.getWriter()); %>
</body>
</html>

Explanation of the code:

Exception_example.jsp

Code Line 1: Here we are setting error page to guru_error.jsp which will be used when the error will be redirected.

Code Line 15: we are taking a variable num and setting it to 10 and checking a condition if num is 10 then to throw a Runtime Exception with the message as Error Condition.

Guru_error.jsp

Code Line 1: Here we are setting isErrorPage attribute to true.

Code Line 12: The exception has been raised in exception_example.jsp using throw object and that exception will be shown here as IsErrorPage attribute is marked as true. Using the exception (this is an object which allows the exception data to be accessed by the JSP.) object we are trying to print the stacktrace of the error which was occurred in exception_example.jsp.

When you execute the above code you get the following output:

Handle Exception in JSP

Output:

The exception has been raised which was thrown from exception_example.jsp using throw object of runtime exception and we get the above code. Also guru_error.jsp is called from which Guru Exception has occurred from this file.

FAQs

JSP exceptions are checked exceptions (like FileNotFoundException, IOException, SQLException), runtime exceptions (like ArrayIndexOutOfBounds, ArithmeticException, NullPointerException), and errors (like stack overflow and internal JVM errors).

The errorPage attribute names the page an exception should be redirected to. The isErrorPage attribute, set to true on that target page, lets it access the exception implicit object and display the error.

The exception implicit object is an instance of Throwable available only on error pages (isErrorPage=true). It exposes methods like getMessage(), getCause(), and printStackTrace() to report the error.

AI can read a stack trace, pinpoint the failing line, explain the root cause in plain language, and suggest a fix, helping developers resolve JSP exceptions faster during testing and code review.

Yes. AI can recommend where to add try-catch blocks or an errorPage, generate a reusable error page, and flag unhandled checked exceptions, improving the robustness of JSP applications.

Summarize this post with: