Java/JSP/Exception

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

Calling Superclass Constructors

<HTML>
    <HEAD>
        <TITLE>Calling Superclass Constructors</TITLE>
    </HEAD>
    <BODY>
        <H1>Calling Superclass Constructors</H1>
        <%!
            javax.servlet.jsp.JspWriter localOut;
            class a
            {
                a() throws java.io.IOException 
                {
                    localOut.println("In a\"s constructor...<BR>");
                }
            }
            class b extends a
            {
                b() throws java.io.IOException 
                {
                    localOut.println("In b\"s constructor...<BR>");
                }
            }
        %>     
        <%
        localOut = out;     
        b obj = new b();
        %>
    </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[10];
        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 i = 1;
            i = i / 0;
            out.println("The answer is " + i);
        %>
    </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 NewException extends Exception 
            {
                int value;
                public String toString() 
                {
                    return "NewException " + value;
                }
                NewException(int v) 
                {
                    value = v;
                }
            }
            void doWork(int value) throws NewException 
            {
                if(value == 0){
                    throw new NewException(value);
            }
        }
    %>
    <%
        try {
            doWork(3);
            doWork(0);
        } catch (NewException e) {
            out.println("Exception: " + e);
        }
    %>
    </BODY>
</HTML>





Runtime Polymorphism

<HTML>
    <HEAD>
        <TITLE>Runtime Polymorphism</TITLE>
    </HEAD>
    <BODY>
        <H1>Runtime Polymorphism</H1>
        <%!
            javax.servlet.jsp.JspWriter localOut;
            class a
            {
                public void print() throws java.io.IOException 
                {
                    localOut.println("Hello from a...<BR>");
                }
            }
            class b extends a
            {
                public void print() throws java.io.IOException 
                {
                    localOut.println("Hello from b...<BR>");
                }
            }
            class c extends a
            {
                public void print() throws java.io.IOException 
                {
                    localOut.println("Hello from c...<BR>");
                }
            }
            class d extends a
            {
                public void print() throws java.io.IOException 
                {
                    localOut.println("Hello from d...<BR>");
                }
            }
        %>     
        <%
            localOut = out;     
    
            a a1 = new a(); 
            b b1 = new b(); 
            c c1 = new c(); 
            d d1 = new d(); 
            a baseClassVariable;
    
            baseClassVariable = a1;
            baseClassVariable.print();
    
            baseClassVariable = b1;
            baseClassVariable.print();
    
            baseClassVariable = c1;
            baseClassVariable.print();
    
            baseClassVariable = d1;
            baseClassVariable.print();
        %>
    </BODY>
</HTML>