ARTICLE

Throwing an Exception in a JSP Page

Posted by Vikas Mishra Articles | JSP January 04, 2012
In this article, I am going to describe how to throw an exception in JSP.
Reader Level:
Download Files:
 

In this article, I am going to describe how to throw an exception in JSP. To throw an exception, the throw keyword is used, followed by the exception object to be thrown. The code to throw an exception looks as follows:


throw new IOException("Some required files are missing");

Some commonly thrown Exceptions

IllegalArgumentException :
If your method only accepts arguments within a particular range, e.g. only positive numbers, then you should check for invalid parameters and throw an IllegalArgumentException.

NullPointerException : If you know that a parameter to your method cannot be null, then it is best to explicitly check for null and throw a NullPointerException.

IllegalStateException : This one is a little less common, but is useful a method relies on a previous method having been called.

UnsupportedOperationException : This exception is designed for cases where you override an abstract class or implement an interface, but don't want or can't to implement certain methods.

Runtime Exception :These essentially mean "that should never happen and I don't know what to do if it does", with different degrees of seriousness. You basically want the application to "bomb out" of whatever it's doing at that moment. If essentially the application can go on working after the unexpected condition, throw a RuntimeException.

Now we are going to develop an JSP application to throw Exception. For this application we follow the following steps

Steps 1: Create New Project

In this step we select a new project option from File menu.


 
new project.jpg

Step 2: Choose Project

In this step we select a Web Application project from the Java Web Categories and click the next button.

new web application.jpg

Step 3: Name and Location

In this step we give a specific name to the project and save it in a specific location then click on the Next button.

name and location.jpg
 

Step 4: Server and Setting

In this step we choose a specific server and Java EE version then click on the next button.

server and setting.jpg

Step 5: Select Framework

There is no need to select any framework in this application, just click on the Next button, after this step the project is open and you are ready for coding.

frameworks.jpg

Step 6: Create JSP file

In this application we create two JSP files; first one is index.jsp and ExceptionthrowMethod.jsp.

create new file.jpg

Index.jsp
 
 
In this JSP file we throw the exception; only throw the throw keyword.
 
 
<html>
        <head>

           <title>Throwing an Exception</title>
        </head>
        <body bgcolor="skyblue">
        <title>Throwing an Exception in a JSP page</title>
        <%
        try
           {
             throw new ArithmeticException("Mathmatics Exception!");
           }
             catch(ArithmeticException e)
           {
             out.println("Exception Message: " + e);
           }
        %>
         </body>
 </html>

ActionThrowMethod.jsp

In this JSP file we throw the exception throw the methods.


 
<HTML>
        <HEAD>

          <TITLE>Throwing Exceptions From Methods</TITLE>
        </HEAD>
        <BODY bgcolor="brown">
            <H1>Throwing Exceptions From Methods in a JSP page</H1>
            <%!
                  void doWork() throws ArrayIndexOutOfBoundsException
                 {
                   int array[] = new int[10];
                   array[200] = 200;
                 }
             %>
             <%
                   try
                      {
                        doWork();
                      }
                         catch (ArrayIndexOutOfBoundsException e) {
                         out.println("Array out of bounds exception");
                      }
             %>
          </BODY>       
 </HTML>


Compiling and Running the application

Now we first compile and then run this application in server and found the given output

Output

index.jsp

index.jsp.jpg


ActionThrowMethod.jsp

method.jsp.jpg

Resources

Here are some useful related resources:

COMMENT USING