exam questions

Exam 1z0-899 All Questions

View all questions & answers for the 1z0-899 exam

Exam 1z0-899 topic 1 question 67 discussion

Actual exam question from Oracle's 1z0-899
Question #: 67
Topic #: 1
[All 1z0-899 Questions]

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
Show Suggested Answer Hide Answer
Suggested Answer: A 🗳️
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"

Comments

Chosen Answer:
This is a voting comment (?). It is better to Upvote an existing comment if you don't have anything to add.
Switch to a voting comment New
Currently there are no comments in this discussion, be the first to comment!
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...