Creating a Custom Exception Object : Exception « JSP « Java Tutorial






<HTML>
    <HEAD>
        <TITLE>Creating a Custom Exception Object</TITLE>
    </HEAD>

    <BODY>
        <H1>Creating a Custom Exception Object</H1>
        <%!
            class MyException extends Exception 
            {
                int value;

                public String toString() 
                {
                    return "MyException " + value;
                }

                MyException(int v) 
                {
                    value = v;
                }
            }

            void doWork(int value) throws MyException 
            {
                if(value == 0){
                    throw new MyException(value);
            }
        }
    %>

    <%
        try {
            doWork(3);
            doWork(2);
            doWork(1);
            doWork(0);
        } 
        catch (MyException e) {
            out.println("Exception: " + e);
        }
    %>
    </BODY>
</HTML>








23.12.Exception
23.12.1.Causing a Runtime Error
23.12.2.Using a try/catch Block
23.12.3.Nesting try/catch Statements
23.12.4.Catching an ArrayIndexOutOfBoundsException Exception
23.12.5.Catching an ArithmeticException Exception
23.12.6.Throwing an Exception
23.12.7.Throwing Exceptions From Methods
23.12.8.Creating a Custom Exception Object
23.12.9.Output error message to console
23.12.10.Printing a Stack Trace to the Server Console