<%@ page isErrorPage='true' %>
The exception object is an instance of a subclass of Throwable (e.g., java.lang. NullPointerException) and is only available in error pages. Following is the list of important methods available in the Throwable class.
Returns a detailed message about the exception that has occurred. This message is initialized in the Throwable constructor.
Returns the cause of the exception as represented by a Throwable object.
Prints the result of toString() along with the stack trace to System.err, the error output stream.
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.
Fills the stack trace of this Throwable object with the current stack trace, adding to any previous information in the stack trace.
The following code demonstrates the use of the exception implicit object. The first page uses the errorPage directive to set up the JSP page to use when an error occurs, and the second page called ErrorPage.jsp uses the isErrorPage directive to set itself up to catch the error.
First page
index.jsp
<%@page errorPage=”ErrorPage.jsp” %>
<%
String name = null;
int i = 9;
int j = 0;
out.print(i/j);
out.print(name.trim());
%>
ErrorPage.jsp
<%@ page isErrorPage='true' %> <% out.print("Error Message : "); out.print(exception.getMessage()); %>
There are two kind of errors, compile-time errors and Runtime errors, can occur in the JSP lifecycle:
JSPs provide robust error-handling capabilities. You can use any Java code error handling techniques, such as try-catch blocks. You can also use the page directive to designate an error page, another JSP that handles the exception.
This approach is better because you don’t need to specify the errorPage attribute in each jsp page. Specifying the single entry in the web.xml file will handle the exception. In this case, either specify exception-type or error-code with the location element. If you want to handle all the exception, you will have to specify the java.lang.Exception in the exception-type element. Let’s see the simple example:
web.xml file if you want to handle any exception
<web-app> <error-page> <exception-type>java.lang.Exception</exception-type> <location>/ErrorPage.jsp</location> </error-page> </web-app>
This approach is better if you want to handle any exception. If you know any specific error code and you want to handle that exception, specify the error-code element instead of exception-type as given below:
web.xml file if you want to handle the exception for a specific error code
<web-app> <error-page> <exception-type>500</exception-type> <location>/ErrorPage.jsp</location> </error-page> </web-app>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…