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
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)- A2% (1)
- B4% (2)
- C11% (5)
- D83% (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;messageis a private field, not a public property. It would needexception.getMessage()to compile, andexceptionis 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 needout.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
Community Discussion
No community discussion yet for this question.