• Home
  • Testing
  • SAP
  • Web
  • Must Learn!
  • Big Data
  • Live Projects
  • Blog

What is JSP Exception?

Exceptions occur when there is an error in the code either by the developer or internal error from the system.

Exception handling in JSP is same as in java where we manage exceptions using try catch blocks.

Unlike Java, there are exceptions in JSP also when there is an error in the code.

Exceptions are of three types:

  1. Checked Exception
  2. RuntimeException
  3. ErrorsException

Checked Exceptions

It is normally a user error or problems which are not seen by the developer are termed as checked exceptions.

Some of the examples are:

  1. FileNotFoundException: This is a checked exception (where it tries to find a file when the file is not found on the disk).
  2. IO Exception: This is also checked exception if there is any exception occurred during reading or writing of a file then the IO exception is raised.
  3. SQLException: This is also a checked exception when the file is connected with SQL database, and there is issue with the connectivity of the SQL database then SQLException is raised

Runtime Exceptions

Runtime exceptions are the one which could have avoided by the programmer. They are ignored at the time of compilation.

Some of the examples are:

  1. ArrayIndexOutOfBoundsException : This is a runtime exception when array size exceeds the elements.
  2. ArithmeticException: This is also a runtime exception when there are any mathematical operations, which are not permitted under normal conditions, for example, dividing a number by 0 will give an exception.
  3. NullPointer Exception: This is also a runtime exception which is raised when a variable or an object is null when we try to access the same. This is a very common exception.

Errors:

The problem arises due to the control of the user or programmer. If stack overflows, then error can occur.

Some examples of the error are listed below:

  1. Error: This error is a subclass of throwable which indicates serious problems that an application cannot catch.
  2. Instantiation error: This error occurs when we try to instantiate an object, and it fails to do that.
  3. Internal Error: This error occurs when there is an error occurred from JVM i.e. Java Virtual Machine.

Error Exception

It is an instance of the throwable class, and it is used in error pages.

Some methods of throwable class are:

  • Public String getMessage() – returns the message of the exception.
  • Public throwablegetCause() – returns cause of the exception
  • Public printStackTrace()- returns the stacktrace of the exception.

Example

Exception_example.jsp

<%@ page errorPage="guru_error.jsp" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Exception Guru JSP1</title>
</head>
<body>
<% 
 int num = 10;
   if (num == 10)
   {
      throw new RuntimeException("Error condition!!!");
   }
 %>
   </body>
</html>

Guru_error.jsp

<%@ page isErrorPage="true" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Guru Exception Page</title>
</head>
<body>
<p>Guru Exception has occurred</p>
<% exception.printStackTrace(response.getWriter()); %>
</body>
</html>

Explanation of the code:

Exception_example.jsp

Code Line 1: Here we are setting error page to guru_error.jsp which will be used when the error will be redirected.

Code Line 15: we are taking a variable num and setting it to 10 and checking a condition if num is 10 then to throw a Runtime Exception with the message as Error Condition.

Guru_error.jsp

Code Line 1: Here we are setting isErrorPageattribute to true.

Code Line 12: The exception has been raised in exception_example.jsp using throw object and that exception will be shown here as IsErrorPage attribute is marked as true. Using the exception (this is an object which allows the exception data to be accessed by the JSP.) object we are trying to print the stacktrace of the error which was occurred in exception_example.jsp.

When you execute the above code you get the following output:

JSP Exception Handling - Checked Exception, Runtime Exception, Errors Exception

Output:

The exception has been raised which was thrown from exception_example.jsp using throw object of runtime exception and we get the above code.

Also guru_error.jsp is called from which Guru Exception has occurred from this file.

Summary:

Here we have learnt about exceptions in this tutorial and taken an example of runtime exception in the tutorial.

 

YOU MIGHT LIKE: