//: c10:FullConstructors.java // From 'Thinking in Java, 2nd ed.' by Bruce Eckel // www.BruceEckel.com. See copyright notice in CopyRight.txt. // Creates an exception class with two constructors: a no-argument // constructor and one that takes a string argument. // Throws two exceptions - one using each constructor - and calls // the printStackTrace method (inherited from base class Exception) // for each MyException object created (i.e., thrown). // In the case where the string-arg constructor was called, the string // parameter passed to the constructor is also printed.. class MyException extends Exception { public MyException() // no-arg constructor {} public MyException(String msg) // string-arg constructor { super(msg); // calls base class constructor and } // passes string parameter to it } public class FullConstructors { public static void f() throws MyException { System.out.println( "Throwing MyException from f()" ) ; throw new MyException() ; // call no-arg constructor } public static void g() throws MyException { System.out.println("Throwing MyException from g()" ) ; // call string-arg constructor throw new MyException("Originated in g()") ; } public static void main(String[] args) { try { f() ; } catch(MyException e) { e.printStackTrace(System.err) ; } try { g() ; } catch(MyException e) { e.printStackTrace(System.err) ; } } } ///:~ /* program output: Throwing MyException from f() MyException at FullConstructors.f(FullConstructors.java:26) at FullConstructors.main(FullConstructors.java:39) Throwing MyException from g() MyException: Originated in g() at FullConstructors.g(FullConstructors.java:33) at FullConstructors.main(FullConstructors.java:46) */