Home Jsp Simple-jsp-example Custom Exceptions



Custom Exceptions
Posted on: March 12, 2008 at 12:00 AM
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.

Related Tags for Custom Exceptions:
cexceptionerrorclassormanttimepropertiesimportiocustommethodhexexecportexerowconditionforieexamplecalltoexecutionexamfetchrunexceptionseilthrowitpeimfromceinnormcalasstamntcaclesdeclareallmehrprocuttiexawhenwhichxampsxebeforeatanykishaimellivmplproperetchetchgoexceptandarcodcodeccrtropewheneverutissrithcondavbelostabhatfeecletcetcpleplpropprndodeonomonormnormanor


More Tutorials from this section

Ask Questions?    Discuss: Custom Exceptions  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.