---
description: In this tutorial, we will learn What is Exception &amp; Exception Handling in JSP, JSP Exception Types &amp; How to Handle Exception in JSP with Try Catch.
title: Exception Handling in JSP
image: https://www.guru99.com/images/exception-handling-in-jsp.png
---

[Skip to content](#main)

**⚡ 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.

Read More

![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](https://www.guru99.com/sql.html) 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](https://www.guru99.com/jsp-elements.html).
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](https://www.guru99.com/java-virtual-machine-jvm.html).

**Don't Miss:**

- [JSP Elements Tag](https://www.guru99.com/jsp-elements.html)
- [JSP Current Date and Time](https://www.guru99.com/jsp-date-handling.html)
- [Difference Between Servlet and JSP](https://www.guru99.com/difference-between-servlets-vs-jsp.html)
- [Top 20 Maven Interview Questions and Answers (2026)](https://www.guru99.com/maven-interview-questions.html)

### 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]()](https://www.guru99.com/images/jsp/022916_0439_JSPExceptio3.png)

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

🗂️ What are the three types of exceptions in JSP?

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

🔀 What is the difference between errorPage and isErrorPage in JSP?

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.

🧩 What is the exception implicit object in JSP?

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.

🤖 How can AI help debug JSP exceptions?

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.

🧠 Can AI suggest exception handling for JSP code?

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:

ChatGPTPerplexityGrokGoogle AI

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.**

##### Sign up for the newsletter

Subscribe for Free

You have successfully subscribed.  
Please check your inbox.

![AI-Newsletter]() Chosen by over **350,000+** professionals

[Scroll to top](#wrapper)

×

Search for:

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/exception-handling-in-jsp.png","url":"https://www.guru99.com/images/exception-handling-in-jsp.png","width":"700","height":"250","caption":"Exception Handling in JSP","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/jsp-exception-handling.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/jsp","name":"JSP"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/jsp-exception-handling.html","name":"Exception Handling in JSP"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/jsp-exception-handling.html#webpage","url":"https://www.guru99.com/jsp-exception-handling.html","name":"Exception Handling in JSP","dateModified":"2026-10-09T17:17:56+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/exception-handling-in-jsp.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/jsp-exception-handling.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/editorial","name":"Editorial Team","url":"https://www.guru99.com/author/editorial","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/favicon-new-logo.png","url":"https://www.guru99.com/images/favicon-new-logo.png","caption":"Editorial Team","inLanguage":"en-US"},"sameAs":["https://www.guru99.com/editorial-team"],"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"articleSection":"JSP","headline":"Exception Handling in JSP","description":"In this tutorial, we will learn What is Exception &amp; Exception Handling in JSP, JSP Exception Types &amp; How to Handle Exception in JSP with Try Catch.","keywords":"jsp","speakable":{"@type":"SpeakableSpecification","cssSelector":[".entry-title",".summary"]},"@type":"Article","author":{"@id":"https://www.guru99.com/author/editorial","name":"Editorial Team"},"dateModified":"2026-10-09T17:17:56+05:30","image":{"@id":"https://www.guru99.com/images/exception-handling-in-jsp.png"},"copyrightYear":"2026","name":"Exception Handling in JSP","subjectOf":[{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"What are the three types of exceptions in JSP?","acceptedAnswer":{"@type":"Answer","text":"JSP exceptions are checked exceptions (like FileNotFoundException, IOException, SQLException), runtime exceptions (like ArrayIndexOutOfBounds, ArithmeticException, NullPointerException), and errors (like stack overflow and internal JVM errors)."}},{"@type":"Question","name":"What is the difference between errorPage and isErrorPage in JSP?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","name":"What is the exception implicit object in JSP?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","name":"How can AI help debug JSP exceptions?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","name":"Can AI suggest exception handling for JSP code?","acceptedAnswer":{"@type":"Answer","text":"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."}}]}],"@id":"https://www.guru99.com/jsp-exception-handling.html#schema-1128302","isPartOf":{"@id":"https://www.guru99.com/jsp-exception-handling.html#webpage"},"publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/jsp-exception-handling.html#webpage"}}]}
```
