Java/JSP/Exception
Содержание
Calling Superclass Constructors
<source lang="java">
<HTML>
<HEAD> <TITLE>Calling Superclass Constructors</TITLE> </HEAD> <BODY>
Calling Superclass Constructors
<%! javax.servlet.jsp.JspWriter localOut; class a { a() throws java.io.IOException { localOut.println("In a\"s constructor...
"); } } class b extends a { b() throws java.io.IOException { localOut.println("In b\"s constructor...
"); } } %> <% localOut = out; b obj = new b(); %> </BODY>
</HTML>
</source>
Catching an ArrayIndexOutOfBoundsException Exception
<source lang="java">
<HTML>
<HEAD> <TITLE>Catching an ArrayIndexOutOfBoundsException Exception</TITLE> </HEAD> <BODY>
Catching an ArrayIndexOutOfBoundsException Exception
<% try { int array[] = new int[10]; array[100] = 100; } catch (ArrayIndexOutOfBoundsException e) { out.println("Array index out of bounds."); } %> </BODY>
</HTML>
</source>
Causing a Runtime Error
<source lang="java">
<HTML>
<HEAD> <TITLE>Causing a Runtime Error</TITLE> </HEAD> <BODY>
Causing a Runtime Error
<% int i = 1; i = i / 0; out.println("The answer is " + i); %> </BODY>
</HTML>
</source>
Creating a Custom Exception Object
<source lang="java">
<HTML>
<HEAD> <TITLE>Creating a Custom Exception Object</TITLE> </HEAD> <BODY>
Creating a Custom Exception Object
<%! 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>
</source>
Runtime Polymorphism
<source lang="java">
<HTML>
<HEAD> <TITLE>Runtime Polymorphism</TITLE> </HEAD> <BODY>
Runtime Polymorphism
<%! javax.servlet.jsp.JspWriter localOut; class a { public void print() throws java.io.IOException { localOut.println("Hello from a...
"); } } class b extends a { public void print() throws java.io.IOException { localOut.println("Hello from b...
"); } } class c extends a { public void print() throws java.io.IOException { localOut.println("Hello from c...
"); } } class d extends a { public void print() throws java.io.IOException { localOut.println("Hello from d...
"); } } %> <% 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>
</source>