< Prev
Next >



JSP exception object





Using the JSP isErrorPage attribute of page directive, we can specify a web page which is displayed when an exception is thrown in a JSP web page. Within the page declared as an error page, we can use the JSP inbuilt exception object to access the exception raised in the original JSP web page. This inbuilt JSP exception object is an object of type java.lang.Throwable.




Methods of Throwable class


Methods Description
String getMessage() This method gets the message related to the thrown exception.
void printStackTrace() This method prints Throwable obect and its the stack trace.
void printStackTrace(PrintStream ps) This method prints the Throwable object to the PrintStream object.
void printStackTrace(PrintWriter pw) This method prints the Throwable object to the PrintWriter object.
String toString() This method returns the description of Throwable object.





An example of using the exception object







JspExceptions.jsp
<%@ page errorPage = "HandlingError.jsp" %>
<html>
<head>	
<title>Using JSP config object</title>
</head>

<body>

<% 
int a=10, b=0, c;
	
c=a/b; 	//ArithmeticException i.e. Divide by zero exception is thrown here
out.println(c);	
%>

<br/>

</body>
</html>





HandlingError.jsp
<%@ page isErrorPage = "true" %>
<html>
<head>	
<title>JSP exception object</title>
</head>

<body>


<h2> Handling JSP exception using exception object </h2>

<% 
out.println("An exception is thrown by the web page - " + exception);
%>

</body>
</html>



Executing the JspExceptions.jsp throws an exception of type - ArithmeticException and this leads to the execution of error page HandlingError.jsp. Let us see how -







Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter



< Prev
Next >
< JSP config object
JSP pagecontext object >

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page

Notifications



COMING SOON
-
PYTHON