Exception Implicit Object in JSP by R4R Team

Exception Implicit Object in JSP with examples:

In this tutorial, we will discuss exception implicit object of JSP. It’s an instance of java.lang.Throwable and frequently used for exception handling in JSP. This object is only available for error pages, which means a JSP page should have isErrorPage to true in order to use exception implicit object. Let’s understand this with the help of below example :
Exception implicit Object Example:
In this example we are taking two integer inputs from user and then we are performing division between them. We have used exception implicit object to handle any kind of exception in the below example.

index.html

<html>

<head>

<title>Enter two Integers for Division</title>

</head>

<body>

<form action="division.jsp">

Input First Integer:<input type="text" name="firstnum" />

Input Second Integer:<input type="text" name="secondnum" />

<input type="submit" value="Get Results"/>

</form>

</body>

</html>


Here we have specified exception.jsp as errorPage which means if any exception occurs in this JSP page, the control will immediately transferred to the exception.jsp JSP page. Note: We have used errorPage attribute of Page Directive to specify the exception handling JSP page (<%@ page errorPage=”exception.jsp” %>).
division.jsp:

<%@ page errorPage="exception.jsp" %>

<%

String num1=request.getParameter("firstnum");

String num2=request.getParameter("secondnum");

int v1= Integer.parseInt(num1);

int v2= Integer.parseInt(num2);

int res= v1/v2;

out.print("Output is: "+ res);

%>
In the below JSP page we have set isErrorPage to true which is also an attribute of Page directive, used for making a page eligible for exception handling. Since this page is defined as a exception page in division.jsp, in case of any exception condition this page will be invoked. Here we are displaying the error message to the user using exception implicit object.

exception.jsp

<%@ page isErrorPage="true" %>

Got this Exception: <%= exception %> 

Leave a Comment:
Search
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!