nerdexam
Oracle

1Z0-900 · Question #8

Which code snippet prints the exception error message as part of the page output?

The correct answer is D. <% System.out.println(e.getMessage()) %>. Important note: The answer key appears to contain an error. Option D is technically incorrect for this question, and explaining it as correct would mislead exam takers. Here's the accurate breakdown: System.out.println() (option D) writes to the server's standard output stream (c

Create Java Web Applications using JSPs

Question

Which code snippet prints the exception error message as part of the page output?

Options

  • A<%= exception.message %>
  • B<c:out value="${requestScope['javax.servlet.error.exception']}"/>
  • C<% exception.getMessage(); %>
  • D<% System.out.println(e.getMessage()) %>

How the community answered

(46 responses)
  • A
    2% (1)
  • B
    4% (2)
  • C
    11% (5)
  • D
    83% (38)

Explanation

Important note: The answer key appears to contain an error. Option D is technically incorrect for this question, and explaining it as correct would mislead exam takers. Here's the accurate breakdown:

System.out.println() (option D) writes to the server's standard output stream (console/log), not to the HTTP response sent to the browser. It also references an undefined variable e instead of the JSP implicit exception object, which would cause a compilation error.

The actual correct answer is B. The <c:out> JSTL tag writes its value directly to the page response output. requestScope['javax.servlet.error.exception'] is a valid standard servlet error attribute, so this snippet genuinely renders exception information on the page.

Why the other options fail:

  • A - <%= exception.message %> uses invalid Java syntax; message is a private field, not a public property. It would need exception.getMessage() to compile, and exception is only available on JSP error pages (isErrorPage="true").
  • C - <% exception.getMessage(); %> calls the method inside a scriptlet but discards the return value. Nothing is written to the response. To output it, you'd need out.print(exception.getMessage()).
  • D - As noted above, System.out.println() targets the server console, not the page.

Memory tip: In JSP, only <%= ... %> (expression tag) and tag library output tags like <c:out> write directly to the page. Scriptlet code (<% ... %>) requires an explicit out.print() call to produce page output. If it looks like a method call with no destination, it's not going to the page.

Topics

#JSP exception handling#JSP implicit objects#JSP syntax#error pages

Community Discussion

No community discussion yet for this question.

Full 1Z0-900 Practice