47 lines
1.4 KiB
Java
47 lines
1.4 KiB
Java
// exceptions/RethrowNew.java
|
|
// ©2016 MindView LLC: see Copyright.txt
|
|
// We make no guarantees that this code is fit for any purpose.
|
|
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
|
|
// Rethrow a different object from the one that was caught.
|
|
|
|
class OneException extends Exception {
|
|
public OneException(String s) { super(s); }
|
|
}
|
|
|
|
class TwoException extends Exception {
|
|
public TwoException(String s) { super(s); }
|
|
}
|
|
|
|
public class RethrowNew {
|
|
public static void f() throws OneException {
|
|
System.out.println("originating the exception in f()");
|
|
throw new OneException("thrown from f()");
|
|
}
|
|
public static void main(String[] args) {
|
|
try {
|
|
try {
|
|
f();
|
|
} catch(OneException e) {
|
|
System.out.println(
|
|
"Caught in inner try, e.printStackTrace()");
|
|
e.printStackTrace(System.out);
|
|
throw new TwoException("from inner try");
|
|
}
|
|
} catch(TwoException e) {
|
|
System.out.println(
|
|
"Caught in outer try, e.printStackTrace()");
|
|
e.printStackTrace(System.out);
|
|
}
|
|
}
|
|
}
|
|
/* Output:
|
|
originating the exception in f()
|
|
Caught in inner try, e.printStackTrace()
|
|
OneException: thrown from f()
|
|
at RethrowNew.f(RethrowNew.java:15)
|
|
at RethrowNew.main(RethrowNew.java:20)
|
|
Caught in outer try, e.printStackTrace()
|
|
TwoException: from inner try
|
|
at RethrowNew.main(RethrowNew.java:25)
|
|
*/
|