2015-09-07 11:44:36 -06:00
|
|
|
// exceptions/Rethrowing.java
|
2016-12-30 17:23:13 -08:00
|
|
|
// (c)2017 MindView LLC: see Copyright.txt
|
2015-11-15 15:51:35 -08:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
2016-09-23 13:23:35 -06:00
|
|
|
// Visit http://OnJava8.com for more book information.
|
2015-06-15 17:47:35 -07:00
|
|
|
// Demonstrating fillInStackTrace()
|
|
|
|
|
|
|
|
public class Rethrowing {
|
|
|
|
public static void f() throws Exception {
|
2015-12-15 11:47:04 -08:00
|
|
|
System.out.println(
|
|
|
|
"originating the exception in f()");
|
2015-06-15 17:47:35 -07:00
|
|
|
throw new Exception("thrown from f()");
|
|
|
|
}
|
|
|
|
public static void g() throws Exception {
|
|
|
|
try {
|
|
|
|
f();
|
|
|
|
} catch(Exception e) {
|
2015-12-15 11:47:04 -08:00
|
|
|
System.out.println(
|
|
|
|
"Inside g(), e.printStackTrace()");
|
2015-06-15 17:47:35 -07:00
|
|
|
e.printStackTrace(System.out);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public static void h() throws Exception {
|
|
|
|
try {
|
|
|
|
f();
|
|
|
|
} catch(Exception e) {
|
2015-12-15 11:47:04 -08:00
|
|
|
System.out.println(
|
|
|
|
"Inside h(), e.printStackTrace()");
|
2015-06-15 17:47:35 -07:00
|
|
|
e.printStackTrace(System.out);
|
|
|
|
throw (Exception)e.fillInStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
|
|
try {
|
|
|
|
g();
|
|
|
|
} catch(Exception e) {
|
|
|
|
System.out.println("main: printStackTrace()");
|
|
|
|
e.printStackTrace(System.out);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
h();
|
|
|
|
} catch(Exception e) {
|
|
|
|
System.out.println("main: printStackTrace()");
|
|
|
|
e.printStackTrace(System.out);
|
|
|
|
}
|
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
}
|
|
|
|
/* Output:
|
2015-06-15 17:47:35 -07:00
|
|
|
originating the exception in f()
|
2015-09-07 11:44:36 -06:00
|
|
|
Inside g(), e.printStackTrace()
|
2015-06-15 17:47:35 -07:00
|
|
|
java.lang.Exception: thrown from f()
|
2015-12-15 11:47:04 -08:00
|
|
|
at Rethrowing.f(Rethrowing.java:8)
|
|
|
|
at Rethrowing.g(Rethrowing.java:12)
|
|
|
|
at Rethrowing.main(Rethrowing.java:32)
|
2015-06-15 17:47:35 -07:00
|
|
|
main: printStackTrace()
|
|
|
|
java.lang.Exception: thrown from f()
|
2015-12-15 11:47:04 -08:00
|
|
|
at Rethrowing.f(Rethrowing.java:8)
|
|
|
|
at Rethrowing.g(Rethrowing.java:12)
|
|
|
|
at Rethrowing.main(Rethrowing.java:32)
|
2015-06-15 17:47:35 -07:00
|
|
|
originating the exception in f()
|
2015-09-07 11:44:36 -06:00
|
|
|
Inside h(), e.printStackTrace()
|
2015-06-15 17:47:35 -07:00
|
|
|
java.lang.Exception: thrown from f()
|
2015-12-15 11:47:04 -08:00
|
|
|
at Rethrowing.f(Rethrowing.java:8)
|
|
|
|
at Rethrowing.h(Rethrowing.java:22)
|
|
|
|
at Rethrowing.main(Rethrowing.java:38)
|
2015-06-15 17:47:35 -07:00
|
|
|
main: printStackTrace()
|
|
|
|
java.lang.Exception: thrown from f()
|
2015-12-15 11:47:04 -08:00
|
|
|
at Rethrowing.h(Rethrowing.java:27)
|
|
|
|
at Rethrowing.main(Rethrowing.java:38)
|
2015-09-07 11:44:36 -06:00
|
|
|
*/
|