To install new stack trace information, we can call fillInStackTrace(), which returns a Throwable object it creates by stuffing the current stack information into the old exception object.
case 1:
use fillInStackTrace() method
// exceptions/Rethrowing.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Demonstrating fillInStackTrace()
public class Rethrowing {
public static void f() throws Exception {
System.out.println("originating the exception in f()");
throw new Exception("thrown from f()");
}
public static void g() throws Exception {
try {
f();
} catch (Exception e) {
System.out.println("Inside g(), e.printStackTrace()");
e.printStackTrace(System.out);
throw e;
}
}
public static void h() throws Exception {
try {
f();
} catch (Exception e) {
System.out.println("Inside h(), e.printStackTrace()");
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);
}
}
}
/* My Output:
originating the exception in f()
Inside g(), e.printStackTrace()
java.lang.Exception: thrown from f()
at Rethrowing.f(Rethrowing.java:10)
at Rethrowing.g(Rethrowing.java:15)
at Rethrowing.main(Rethrowing.java:35)
main: printStackTrace()
java.lang.Exception: thrown from f()
at Rethrowing.f(Rethrowing.java:10)
at Rethrowing.g(Rethrowing.java:15)
at Rethrowing.main(Rethrowing.java:35)
originating the exception in f()
Inside h(), e.printStackTrace()
java.lang.Exception: thrown from f()
at Rethrowing.f(Rethrowing.java:10)
at Rethrowing.h(Rethrowing.java:25)
at Rethrowing.main(Rethrowing.java:41)
main: printStackTrace()
java.lang.Exception: thrown from f()
at Rethrowing.h(Rethrowing.java:29)
at Rethrowing.main(Rethrowing.java:41)
*/
fillInStackTrace
public Throwable fillInStackTrace()
Fills in the execution stack trace. This method records within this Throwable
object information about the current state of the stack frames for the current thread.
If the stack trace of this Throwable
is not writable, calling this method has no effect.
Returns:
a reference to this Throwable
instance.
See Also:
case 2:
rethrow a different object from the one we caught.
// exceptions/RethrowNew.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Rethrow a different object from the one you caught
class OneException extends Exception {
OneException(String s) {
super(s);
}
}
class TwoException extends Exception {
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);
}
}
}
/* My Output:
originating the exception in f()
Caught in inner try, e.printStackTrace()
OneException: thrown from f()
at RethrowNew.f(RethrowNew.java:22)
at RethrowNew.main(RethrowNew.java:28)
Caught in outer try, e.printStackTrace()
TwoException: from inner try
at RethrowNew.main(RethrowNew.java:32)
*/
Don’t worry about cleaning up the previous exception, or any exceptions. They’re all heap-based objects created with new, so the garbage collector automatically cleans them all up.
references:
1. On Java 8 - Bruce Eckel
2. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/exceptions/Rethrowing.java
3. https://docs.oracle.com/javase/8/docs/api/java/lang/Throwable.html#fillInStackTrace--
4. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/exceptions/RethrowNew.java