JSP

exception implicit object

The exception object represents all errors and exceptions. The exception implicit object is of type java.langThrowable. You can access the exception object on a page that you declare to be an error page using the isErrorPage attribute of the page directive. Note that the exception object is created only if the JSP uses the page directive to set isErrorPage set to true. When a JSP generates an error and forwards that error to the error page, it sets the JSP exception object of the error page to the generated error
<%@ page isErrorPage='true' %>

Using Exception Object:

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.

public String getMessage():

Returns a detailed message about the exception that has occurred. This message is initialized in the Throwable constructor.

public Throwable getCause():

Returns the cause of the exception as represented by a Throwable object.

public void printStackTrace():

Prints the result of toString() along with the stack trace to System.err, the error output stream.

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.

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.

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:

  1. Compile-time errors : Compile-time errors occur when a client first invokes a JSP. JRun transforms the JSP into its XML view, and then compiles a servlet from the XML view. If an error occurs at any time during this process, a compile-time error is thrown. You must catch that error outside of the JSP. However, if you do not test or precompile the JSP, you might not catch the error at all.
  2. Runtime errors : Runtime errors occur when a JSP successfully compiles but then tries to process code that contains an invalid statement; for example, when you try to include a nonexistent file. Runtime errors are easy to handle because you know that the JSP compiled successfully, you just need to catch the error and examine the output.

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.

Example of exception handling in jsp by specifying the error-page element in web.xml file

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 for specifying the error-page element
  • index.jsp for input values
  • index.jsp for dividing the two numbers and displaying the result
  • ErrorPage.jsp for displaying the exception

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>
<<Previous <<   || Index ||   >>Next >>
Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

3 years ago