Exception Handling in JSP

In the previous article, we have seen the Expression language in JSP with a simple example. Now In this article, we will discuss Exception Handling in JSP.

As we know, Exception is an unwanted event that arises during the execution of a program, therefore these need to be handled in the program. So, In this article, we will discuss How to handle Exception on the JSP page.

What is JSP Exception?

The Exception in JSP is an abnormal condition that arises during the execution of a program that interrupts the normal flow of execution.

  • The exception is handled in JSP the same as that in JAVA programming by using a try-catch block.
  • The exception is handled at runtime.
  • We should always Handle Exception to be on the safer side for the web developer.

Checked Exception

A checked exception is an exception that is checked at compile time. This exception is typically a user error that cannot be foreseen by the programmer. some checked exceptions are

  • FileNotFoundExeption: This exception is thrown when we try to open a file that is not present int the system.
  • IOException: This exception is thrown during reading and writing files.
  • SQLException: This exception is thrown when there is an issue with the connectivity of the SQL database.

Runtime Exception

A runtime exception is an exception that could have been avoided by the developer. It is generally ignored at compile time.

  • NumberFormatException: The NumberFormatException is thrown when we try to convert a string into a numeric value such as float or integer, but the format of the input string is not appropriate or illegal.
  • ArithmeticException: This exception is thrown when there is any mathematical operation that is not permitted.
  • NullPointerException: This exception is thrown when we try to access the null object. 

Error Exception:

The problem that arises in the program which is beyond the control of the developer. It is generally ignored by the user. for example stack overflows errors arise. Error exception is an instance of a throwable class in JAVA.

Methods of Throwable class

  • public String getMessage(): This method returns the detailed message of the exception raised.
  • public Throwable getCause(): This method returns the cause of the exception.
  • public String toString(): This method returns the class name concatenated with getMessage().
  • public void printStackTrace(): This method prints the stack trace of the exception.
  • public StackTraceElement [ ] getStackTrace(): This method returns the array of stack trace of the exception.
  • public Throwable fillInStackTrace(): This method fills the stack trace with the current trace.

Example of exception handling in the JSP

In this example, we have created a JSP page that will cause Exception. Also created a separate error page which will display the exception.

Here we have to use the errorPage directive that we have discussed earlier in the directive article.

index.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>Insert title here</title>
</head>
<body bgcolor="#A5D8F3">
<form action="second.jsp">
num1 <input type="text" name="num1"><br><br>
num2 <input type="text" name="num2"><br>
<input type="submit" value="Divide It">
</form>
</body>
</html>

second.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ page errorPage="error.jsp" %>
<!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>Insert title here</title>
</head>
<body>
<%
int num1=Integer.parseInt(request.getParameter("num1"));
int num2=Integer.parseInt(request.getParameter("num2"));
int num3=num1/num2;
out.print("num3 is : "+num3);
%>
</body>
</html>

error.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page isErrorPage="true" %>
<!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>Insert title here</title>
</head>
<body bgcolor="#A5D8F3">
<h3>Exception occured</h3>
Exception : <%= exception %>
</body>
</html>

Output

Thus we have discussed the Exception in JSP with an example of a runtime exception.

In the next article of this tutorial, we will discuss the JSP Standard Tag Library and its classification with a simple example.