JSP Exception Implicit object

  • The exception object represents all errors and exceptions.
  • The exception implicit object is of type java.langThrowable.
  • You can access the exception object on a page thatyou declare to be an error page using the isErrorPage attribute of the page directive.

Note:
The exception object is created only if the JSP uses the page directive to set isErrorPage set to true.
When a JSP generates an error and forwards that error to the error page, the container sets the
JSP exception object of the error page to the generated error

Apart from exception.getMessage(),
you can also invoke printStackTrace() and toString() on exception object.

Let’s try to Understand it with Example

project Structure

ExceptionPage.jsp


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 
<%@ page language="java"%>
<%@ page errorPage="err.jsp" %>
 
<html>
<head>
<title>Insert title here</title>
</head>
<body>
 
<%
int a=5/0;
 
out.println(a);
 
System.out.print("Exception occur");
 
%>
 
</body>
</html>

err.jsp


1
2
3
4
5
6
7
8
9
10
11
12
 
<%@ page language="java"%>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
 
Sorry something Goes Wrong........
 
</body>
</html>

output:

Leave a Reply

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