Prev Question
Next Question

You are working on a JSP that is intended to inform users about critical errors in the system. The JSP code is attempting to access the exception that represents
the cause of the problem, but your IDE is telling you that the variable does not exist. What should you do to address this problem?

A.
Add a page directive stating that this page is an error handler

B.
Add scriptlet code to create a variable that refer to the exception

C.
Add a <jsp:useBean tag to declare the and access the exception>

D.
Perform the error handling in a servlet rather than in the JSP

E.
Edit the page that caused the error to ensure that it specifies this page as its error handler

Explanation:
Exception is a JSP implicit variable
The exception variable contains any Exception thrown on the previous JSP page with an errorPage directive that forwards to a page with an isErrorPage directive.
Example:
If you had a JSP (index.jsp) which throws an exception (I have deliberately thrown a NumberFormatException by parsing a String, obviously you wouldn’t write a
page that does this, its just an example)
<%@ page errorPage=”error.jsp” %>
<% Integer.parseInt(“foo”); //throws an exception %> This will forward to error.jsp,
If error.jsp was
<%@ page isErrorPage = “true”%>
<body>
<h2>Your application has generated an error</h2>
<h3>Please check for the error given below</h3>
<b>Exception:</b><br>
<font color=”red”><%= exception.toString() %></font> </body>
Because it has the
<%@ page isErrorPage = “true”%>
page directive, the implicit variable exception will contain the Exception thrown in the previous jsp
So when you request index.jsp, the Exception will be thrown, and forwarded to error.jsp which will output html like this
<body>

<h2>Your application has generated an error</h2>
<h3>Please check for the error given below</h3>
<b>Exception:</b><br>
<font color=”red”>java.lang.NumberFormatException: For input string: “foo”</font> </body>
As @JB Nizet mentions exception is an instanceof Throwable calling exception.getMessage() For input string: “foo” instead of java.lang.NumberFormatException:
For input string: “foo”

Prev Question
Next Question

Leave a Reply

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