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.
<%@page errorPage="url of the error page"%>
- isErrorPage : Used to mark a page as an error page where exceptions are displayed.
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:
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 eah page can be spefied in the web.xml file, using the <error-page> element. The syntax is as follows:
The following example illustrates using this technique to handle exceptions:
The output, in this case, is similar as in the previous one.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Exception Handling in PHP
- Exception Handling in Node
- Spring MVC with JSP View
- getParameter() - Passing data from client to JSP
- Program to validate a user using JSP
- getAttribute() - Passing data from Server to JSP
- Introduction to JSP
- Environment setup for JSP
- Life cycle of JSP
- Difference between Servlet and JSP
- Directives in JSP
- JSP | Declaration Tag
- JSP | ScriptletTag
- JSP | Expression tag
- JSP | Implicit Objects - request and response
- JSTL | JSP Standard Tag Library
- Difference between JSP and ASP
- JSP Access Model
- How to display search result of another page on same page using ajax in JSP?
- Classes of JSP API
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai