JSP Exception Implicit Object

exception implicit objects
 1.    The JSP implicit exception object is an instance of thejava.lang.Throwable class.
2.      JSP gives you an option to specify Error Page for each JSP. Whenever the page throws an exception, the JSP container automatically invokes the error page.
3.     And is only available in error pages. Following is the list of important methods available in the Throwable class..
Methods:

1.public String getMessage()

Returns a detailed message about the exception that has occurred. This message is initialized in the Throwable constructor.
2. public Throwable getCause()

Returns the cause of the exception as represented by a Throwable object.
3.public String toString()

Returns the name of the class concatenated with the result of getMessage()
4.public void printStackTrace()

Prints the result of toString() along with the stack trace to System.err, the error output stream.
5.public StackTraceElement [] getStackTrace()

Returns an array containing each element on the stack trace. The element at index 0 represents the top of the call stack, and the last element in the array represents the method at the bottom of the call stack.
6.public Throwable fillInStackTrace()

Fills the stack trace of this Throwable object with the current stack trace, adding to any previous information in the stack trace.


Example: main.jsp 
<%@ page errorPage="ShowError.jsp" %>
<html>
<head>
   <title>Error Handling Example</title>
</head>
<body>
<%
   // Throw an exception to invoke the error page
   int x = 1;
   if (x == 1)
   {
      throw new RuntimeException("Error condition!!!");
   }
%>
</body>
</html>

ShowError.jsp 
<%@ page isErrorPage="true" %>
<html>
<head>
<title>Show Error Page</title>
</head>
<body>
<h1>Opps...</h1>
<p>Sorry, an error occurred.</p>
<p>Here is the exception stack trace: </p>
<pre>
<% exception.printStackTrace(response.getWriter()); %>
</pre>
</body>
</html>

Output: 
java.lang.RuntimeException: Error condition!!!
......
Opps...
Sorry, an error occurred.
Here is the exception stack trace:

Thanks For Making This Possible! Kindly Bookmark and Share it.

Technorati Digg This Stumble Stumble Facebook Twitter

3 comments: