Master java skills

exception Implicit Object

exception is the implicit object of Throwable class. This implicit object is used for exception handling in jsp pages.

Note -> exception implicit object can only be used in error pages.

To handle exception in jsp pages, two attributes of page directives is used: isErrorPage, errorPage

There is another way of handling exceptions in jsp pages. That is by using <error-page> element in web.xml file.

To understand about exception implicit object in more detail, we need to learn about Directive elements. Below is a glimplse of how exception is used in jsp page. To understand the full example, we will do so in coming chapters.

<%@ page errorPage="error.jsp" %>
<html>
  <body>
    <% 
      int[] intArr = {1, 2, 3, 4, 5};
      out.println(intArr[6]); // This will throw ArrayIndexOutOfBoundsException
    %>
  </body>
</html>

error.jsp

<%@ page isErrorPage="true" %>
<html>
  <body>
    <h2>An error occurred:</h2>
    <p><b>Exception Type is:</b> <%= exception.getClass().getName() %></p>
    <p><b>Exception Message is:</b> <%= exception.getMessage() %></p>
    <p><b>Following is the Stack Trace:</b></p>
    <pre>
        <%= exception.printStackTrace(new java.io.PrintWriter(out)) %>
    </pre>
  </body>
</html>