/** * Copyright (c): Uwe Schmidt, FH Wedel * * You may study, modify and distribute this source code * FOR NON-COMMERCIAL PURPOSES ONLY. * This copyright message has to remain unchanged. * * Note that this document is provided 'as is', * WITHOUT WARRANTY of any kind either expressed or implied. */ /** * ein Testprogramm fuer Ausnahmen */ import java.io.IOException; //-------------------- public class ExceptionTest { public static void main(String[] argv) { int i = 0; try { i = Integer.parseInt(argv[0]); } catch ( Exception e ) { } test(i); } //-------------------- public static void test(int i) { System.out.println("test: case = " + i); //-------------------- tryblock: try { switch ( i ) { case 1: System.out.println("test: return inside try block"); return; case 2: System.out.println("test: break out of try block"); break tryblock; case 3: System.out.println( "test: throw new Exception(\"a test\")"); throw new Exception("a test"); case 4: System.out.println( "test: throw new MyException(\"my test\")"); throw new MyException("my test"); case 5: System.out.println("test: call foo"); foo(); break; case 6: System.out.println("test: call bar"); bar(); break; case 7: System.out.println("test: call wow"); wow(); break; case 8: System.out.println("test: call tee with 10"); tee(10); break; case 9: System.out.println("test: call bad"); bad(); break; default: System.out.println("test: normal exit of try block"); } } //-------------------- catch ( MyException e ) { Throwable cause; System.out.println( "test: caught MyException: " + e + ( (cause = e.getCause()) != null ? " cause: " + cause : "" ) ); e.printStackTrace(System.out); } catch ( RuntimeException e ) { System.out.println( "test: caught RuntimeException: " + e); e.printStackTrace(System.out); } catch ( Exception e ) { System.out.println( "test: caught Exception: " + e); e.printStackTrace(System.out); } finally { System.out.println( "test: exec finally clause"); } //-------------------- System.out.println("test: normal exit"); } //-------------------- public static void foo() throws MyException { try { System.out.println( "foo : throw new MyException(\"foo test\")"); throw new MyException("foo test"); } finally { System.out.println("foo : exec finally clause"); } } //-------------------- public static void bar() { System.out.println( "bar : throw new IllegalArgumentException(\"bar test\")"); throw new IllegalArgumentException("bar test"); } //-------------------- public static void wow() throws MyException { int a, b=1, c=0; try { System.out.println( "wow : division by 0"); a = b/c; } catch (Exception e) { System.out.println( "wow : caught Exception: " + e); System.out.println( "wow : throw new MyException(\"wow test\")"); throw // throws again new MyException("wow test", e); } } //-------------------- public static void tee(int i) throws MyException { try { System.out.println( "tee : called with " + i); if ( i <= 0 ) { System.out.println( "tee : throw new MyException(\"tee test\")"); throw new MyException("tee test"); } else { tee(i-1); } } finally { System.out.println("tee : exec finally clause, i = " + i); } } public static void bad() throws MyException { try { System.out.println( "bad: throw new IOException(\"bad test\")"); throw new IOException("bad test"); } finally { System.out.println("bad: exec finally clause"); System.out.println( "bad: throw new MyException(\"bad finally test\")"); throw new MyException("bad finally test"); } } } //-------------------- class MyException extends Exception { public MyException(String s) { super(s); } public MyException(String s, Throwable cause) { super(s, cause); } } //--------------------