Home Jsp Simple-jsp-example Custom Exceptions
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Custom Exceptions

Advertisement
Before going to understand what is Custom Exception, its important to know what is Exception. An Exception occurs whenever an abnormal condition arises at the execution of the code at run time. It is run time error.

Custom Exceptions 

        

Before going to understand what is Custom Exception, its important to know what is Exception. An Exception occurs whenever an abnormal condition arises at the execution of the code at run time. It is run time error. 

Custom Exception inherits the properties from the Exception class. Whenever we have declare our own exceptions then we call it custom exceptions. In the example given below we have declared our own Custom Exception which will be thrown when there arises any exception in the method fetchException().

The code of the program is given below:

<HTML>
    <HEAD>
        <TITLE>Custom Exception in jsp</TITLE>
    </HEAD>
    <BODY>
        <H1>Custom Exception in jsp</H1>
        <%!
            class CustomException extends Exception 
            {
                String value="This is a custom Exception";
                public String toString() 
                {
                    return "Custom Exception: " + value;
                }
                CustomException(String v) 
                {
                    value = v;
                }
            }
     void fetchException(String value) throws CustomException 
            {
                if(value != ""){
                    throw new CustomException(value);
            }
        }
    %>
    <%
        try {
            fetchException("This is a custom Exception");
            fetchException("");
        } catch (CustomException e) {
            out.println("Exception: " + e);
        }
    %>
    </BODY>
</HTML>

Output of the program:

Download this example.

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: March 12, 2008

Related Tutorials

Ask Questions?    Discuss: Custom Exceptions  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Hari
February 15, 2012
Very Useful

The tutorial is been very useful. Thanks
DMCA.com