Java/JSP/Try Catch — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 07:10, 1 июня 2010

Multiple Catch

<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.");
    } catch(ArithmeticException e) {
        out.println("Arithmetic exception: " + e);
    } catch(Exception e) {
        out.println("An error occurred: " + 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>





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>





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 i = 1;
            i = i / 0;
            out.println("The answer is " + i);
        }
        catch (Exception e){
            out.println("An exception occurred: " + e.getMessage());
        }
    %>
    </BODY>
</HTML>