packageexceptions;//: exceptions/Rethrowing.java//Demonstrating fillInStackTrace()
public classRethrowing {public static void f() throwsException {
System.out.println("originating the exception in f()");throw new Exception("thrown from f()");
}public static void g() throwsException {try{
f();
}catch(Exception e) {
System.out.println("Inside g(),e.printStackTrace()");
e.printStackTrace(System.out);throwe;
}
}public static void h() throwsException {try{
f();
}catch(Exception e) {
System.out.println("Inside h(),e.printStackTrace()");
e.printStackTrace(System.out);throw(Exception)e.fillInStackTrace();
}
}public static voidmain(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);
}
}
}/*Output:
originating the exception in f()
Inside g(),e.printStackTrace()
java.lang.Exception: thrown from f()
at Rethrowing.f(Rethrowing.java:7)
at Rethrowing.g(Rethrowing.java:11)
at Rethrowing.main(Rethrowing.java:29)
main: printStackTrace()
java.lang.Exception: thrown from f()
at Rethrowing.f(Rethrowing.java:7)
at Rethrowing.g(Rethrowing.java:11)
at Rethrowing.main(Rethrowing.java:29)
originating the exception in f()
Inside h(),e.printStackTrace()
java.lang.Exception: thrown from f()
at Rethrowing.f(Rethrowing.java:7)
at Rethrowing.h(Rethrowing.java:20)
at Rethrowing.main(Rethrowing.java:35)
main: printStackTrace()
java.lang.Exception: thrown from f()
at Rethrowing.h(Rethrowing.java:24)
at Rethrowing.main(Rethrowing.java:35)*///:~