Exception Handling in JSP

In the previous tutorial, we have learned about JSP Directives. In this tutorial, we will learn JSP Exception Handling. Before discussing Exception Handling in JSP, let's first understand what is Exception and how it is different from errors.


What is Exception?

An exception is an event, that occurs during the execution of a program, that disrupts the normal flow of program instructions.when an error occurs within a method, the methods create objects and hands it off to the runtime system. The object called an exception object. It contains the information about the error, including its type and state of the program when the error occurred. 
Exception Handling is the process of handle the errors that may occur during runtime.  

What is Error?

Most of the times errors are not caused by our program these are caused by system resources. We cannot handle the errors.
For example: if OutOfMemory occurs being a programmer we can't do anything and the program will be terminated automatically. for the complete tutorial visit at Exception Handling in Java

In JSP, we can handle the exception by using two attributes:

1. errorPage: 

This attribute is used to inform the container to jump the control to another JSP page if an exception occurs in the current JSP page. The another JSP page is used to handle the exception that is occurred in the current JSP page.
for example: <%@ errorPage = "error.jsp" %>

2. isErrorPage:

This attribute is used to inform the container that makes a JSP as an error page or not. The default value of this attribute is false. It means a JSP page is not acting an error page by default. To make a JSP page as an error page than we should write isErrorPage = "true" If isErrorPage = "true" then only execution objects is allowed into a JSP page.
For example: <%@ isErrorPage="true" %>

Example of Exception Handling in JSP:

In this example, we are creating the 3 files.
1. index.jsp: for input values.
2. calculation.jsp for dividing the two numbers and display the result.
3. error.jsp for handle the jsp exception.

   index.jsp
JSP Exception Handling


                  calculation.jsp
JSP Exception Handling


                   error.jsp
JSP Exception Handling


       Output:
JSP Exception Handling



JSP Exception Handling



You may also like:
Life Cycle of a JSP Page
JSP Scripting Elements with examples
9 Implicit Objects in JSP
JSP Directives: Page, include and Taglib Directives
Exception Handling in JSP Exception Handling in JSP Reviewed by Prashant Srivastava on December 18, 2018 Rating: 5

No comments:

Powered by Blogger.