When you write code in JSP, you can make some errors while writing lines of code. These errors can be classified into different types. But some errors occur when the code is not logically accurate or an internal error from the system. The concept of handling exceptions to JSP is similar to Java, where exceptions are managed using try-catch blocks. In this tutorial, you will learn about the concept of handling exception in JSP.



Exceptions in JSP

Exceptions can be defined as an object that is thrown during a program run. Exception handling is the practice of handling errors at runtime. Exceptions can occur at any time in a web application. Therefore, the web developer must handle exceptions to be on the safe side and make the user work flawlessly on the web.

The exception Implicit Object

  • An exception implicit object is implemented to handle exceptions to display error messages.
  • The exception implicit object is an instance of the java.lang.Throwable class.
  • It is only available for JSP pages, with the isErrorPage value set as "True". This means Exception objects can only be used in error pages.

Types of Exceptions in JSP

There are three types of exceptions in JSP; these are:
  • Checked Exception
  • Runtime Exception
  • Errors Exception

Let us now discuss each of them in detail.

Checked Exceptions

"Checked exceptions" are a type of exception that is usually a user fault or a problem that cannot be foreseen by the programmer. This type of exception is checked at compile-time. Here is a list of some common "Checked exceptions" that occur in the programming domain:

  • IO Exception: This is an example of a checked exception where some exceptions may occur while reading and writing a file. If the file format is not supported, then the IO exception will occur.
  • FileNotFoundException: This is an example of a checked exception where a file in which data needs to be written is not found on that particular path on the disk.
  • SQLException: This is also an example of a checked exception where the file is associated with a SQL database. The exception will be if there is a problem or concern with the connectivity of the SQL database.

Runtime Exceptions

"Runtime exceptions" can be defined as exceptions evaded by the programmer. They are left unnoticed at compilation time. Here is the list of some of the common examples that occurred in the programming domain:

  • ArrayIndexOutOfBoundsException: This is a runtime exception; This occurs when the array size goes beyond the elements or index value set.
  • NullPointer Exception: This is also an example of a runtime exception raised when a variable or object is empty or null, and users or programmers try to access it.
  • ArithmeticException: This is an example of a runtime exception when a mathematical operation is not allowed under normal circumstances. A common example of such a scenario is to divide the number by 0.

Errors

"Error exception" arises solely because of the user or programmer. Here you might encounter error due to stack overflows. Here is the list of some of the common examples that occurred in the programming domain:

  • Error: This is a subclass of throwable that indicates serious problems the applications cannot catch.
  • Internal Error: This error occurs when a fault occurs in the Java Virtual Machine (JVM).
  • Instantiation error: This error occurs when you try to instantiate an object, ultimately failing to do that.

ArithmeticException Example

Example (HTML file):
<!DOCTYPE html>
<html>
    <head>
        <title>Enter two Integers for Division</title>
    </head>
    <body>
        <form action="submit.jsp">
            Insert first Integer: <input type="text" name="numone" /><br />
            Insert second Integer: <input type="text" name="numtwo" /><br />
            <input type="submit" value="Get Results" />
        </form>
    </body>
</html>
Example (submit.jsp):
<%@ page errorPage="exception.jsp" %>
  
<% 
String num1 = request.getParameter("numone"); 
String num2 = request.getParameter("numtwo"); 
int var1= Integer.parseInt(num1);
int var2= Integer.parseInt(num2);
int r= var1 / var2;
out.print("Output is: "+ r);
%>
Example (exception.jsp):
<%@ page isErrorPage='true' %>
  
<% 
out.print("Error Message : ");  
out.print(exception.getMessage());
%>


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram