package exception; public class Examples { public class MyException extends Exception { public MyException (String message) { super (message); } } private Object object; /** Constructs Examples. */ public Examples () { try { // trial (); // handlesException (); // passesOnException (); // handelsAndThrowsException (); handelsAndThrowsMyException (); System.err.println ("the end"); } catch (Exception error) { System.err.println ("some error occured in Examples"); error.printStackTrace (); } catch (Error error) {} //never ignore Exceptions or Errors! } private void handelsAndThrowsMyException () throws MyException { try { int i = 10 / 0; } catch (ArithmeticException error) { System.err.println ("handelsAndThrowsMyException: handled division by zero"); throw new MyException ("This was an intentional division by zero"); } } private void handelsAndThrowsException () { try { int i = 10 / 0; } catch (ArithmeticException error) { System.err.println ("handelsAndThrowsException: handled division by zero"); throw error; } } private void passesOnException () throws ArithmeticException{ int i = 10 / 0; System.err.println ("passesOnException"); } private void handlesException () { try { int i = 10 / 0; } catch (ArithmeticException error) { System.err.println ("handlesException: handled division by zero"); } } private void trial () throws MyException { try { // System.err.println (object.toString ()); // int i = 10 / 0; // String text = "12345".substring (10); // System.out.println ("int number=" + Integer.parseInt ("5.0")); char ch = "12345".charAt (5); System.out.println ("ch = '" + ch + "'"); // throw new MyException ("dummy one!"); System.err.println ("this code is executed only if no exception is thrown"); } catch (NullPointerException error) { System.err.println ("object is null"); System.err.println ("message = " + error.getMessage ()); error.printStackTrace (); } catch (ArithmeticException error) { System.err.println ("division by zero"); error.printStackTrace (); } } public static void main (String [] args) { new Examples (); } }