Custom Exceptions

Ads
 

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.

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.

Ads

Ads