Java Tutorial/JSP/Exception

Материал из Java эксперт
Версия от 05:07, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Catching an ArithmeticException Exception

<HTML>
    <HEAD>
        <TITLE>Catching an ArithmeticException Exception</TITLE>
    </HEAD>
    <BODY>
        <H1>Catching an ArithmeticException Exception</H1>
    <%
    try {
        int array[] = new int[100];
        array[100] = 100;
    } catch (ArrayIndexOutOfBoundsException e) {
        out.println("Array index out of bounds.");
    } catch(ArithmeticException e) {
        out.println("Arithmetic exception: " + e);
    } catch(Exception e) {
        out.println("An error occurred: " + e);
    }

    %>
    </BODY>
</HTML>





Catching an ArrayIndexOutOfBoundsException Exception

<HTML>
    <HEAD>
        <TITLE>Catching an ArrayIndexOutOfBoundsException Exception</TITLE>
    </HEAD>
    <BODY>
        <H1>Catching an ArrayIndexOutOfBoundsException Exception</H1>
    <%
    try {
        int array[] = new int[100];
        array[100] = 100;
    } catch (ArrayIndexOutOfBoundsException e) {
        out.println("Array index out of bounds.");
    }
    %>
    </BODY>
</HTML>





Causing a Runtime Error

<HTML>
    <HEAD>
        <TITLE>Causing a Runtime Error</TITLE>
    </HEAD>
    <BODY>
        <H1>Causing a Runtime Error</H1>
        <%
            int int1 = 1;
            int1 = int1 / 0;
            out.println("The answer is " + int1);
        %>
    </BODY>
</HTML>





Creating a Custom Exception Object

<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>





Nesting try/catch Statements

<HTML>
    <HEAD>
        <TITLE>Nesting try/catch Statements</TITLE>
    </HEAD>
    <BODY>
        <H1>Nesting try/catch Statements</H1>
        <%
        try {
            try {
                int c[] = {0, 1, 2, 3};
                c[4] = 4;
            } catch(ArrayIndexOutOfBoundsException e) {
                out.println("Array index out of bounds: " + e);
            }
        } catch(ArithmeticException e) {
            out.println("Divide by zero: " + e);
        }
        %>
    </BODY>
</HTML>





Output error message to console

<HTML>
    <HEAD>
        <TITLE>Building a simple error handling page.</TITLE>
    </HEAD>
    <BODY>
        <%
            try{
                int value = 1;
                value = value / 0;
            }
            catch (Exception e){
                System.out.println(e.getMessage());
            }
        %>
    </BODY>
</HTML>





Printing a Stack Trace to the Server Console

<HTML>
    <HEAD>
        <TITLE>Printing a Stack Trace to the Server Console</TITLE>
    </HEAD>
    <BODY>
        <H1>Printing a Stack Trace to the Server Console</H1>
        <%
            try{
                int value = 1;
                value = value / 0;
            }
            catch (Exception e){
                e.printStackTrace();
            }
        %>
    </BODY>
</HTML>





Throwing an Exception

<html>
    <head>
        <title>Throwing an Exception</title>
    </head>
    <body>
        <title>Throwing an Exception</title>
        <%
        try {
            throw new ArithmeticException("Math Exception!");
        } catch(ArithmeticException e) {
            out.println("Exception message: " + e);
        }
        %>
    </body>
</html>





Throwing Exceptions From Methods

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





Using a try/catch Block

<HTML>
    <HEAD>
        <TITLE>Using a try/catch Block</TITLE>
    </HEAD>
    <BODY>
        <H1>Using a try/catch Block</H1>
    <%
        try{
            int int1 = 1;
            int1 = int1 / 0;
            out.println("The answer is " + int1);
        }
        catch (Exception e){
            out.println("An exception occurred: " + e.getMessage());
        }
    %>
    </BODY>
</HTML>