The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
January 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

loss of stacktrace on rethrow

Posted by Brian Wortman on January 17, 2001 at 11:05 AM

Hello,

We were having some problems with loss of stacktrace, so we decided to try to run Bruce Eckel's "Rethrowing" example (chapter 10, Thinking in Java). When we run Rethrowing we get the expected results. However, if we call Rethrowing.g() from another class (in a different .java file) we get the following - which is consistent with the problem we're seeing in our code:

originating the exception in f()
Inside g(), e.printStackTrace()
java.lang.Exception: thrown from f()
at com.rds.framework.persistence.Rethrowing.f(rethrowing.java:12)
at com.rds.framework.persistence.Rethrowing.g(rethrowing.java:16)
at com.rds.framework.persistence.Rethrower.main(rethrower.java:12)
Caught in main, e.printStackTrace()
java.lang.Exception: thrown from f()
at com.rds.framework.persistence.Rethrowing.g(rethrowing.java:21)
at com.rds.framework.persistence.Rethrower.main(rethrower.java:12)

Is this normal, or is JBuilder non-conforming? If this is normal, how can we preserve our full stacktrace (i.e.., tracing all the way back to the originating throw) when throwing across files?

If you want to see the actual code we used, here it is:

//: c10:Rethrower.java
// Adapted From 'Thinking in Java, 2nd ed.' by Bruce Eckel
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
// Demonstrating fillInStackTrace()


public class Rethrower {
public static void
main(String[] args) throws Throwable {
try {
Rethrowing.g();
} catch(Exception e) {
System.err.println(
"Caught in main, e.printStackTrace()");
e.printStackTrace(System.err);
}
}
}

/////////////////////////////////////////////////////////////

//: c10:Rethrowing.java
// Adapted From 'Thinking in Java, 2nd ed.' by Bruce Eckel
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
// 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 Throwable {
try {
f();
} catch(Exception e) {
System.err.println(
"Inside g(), e.printStackTrace()");
e.printStackTrace(System.err);
throw e; // 17
//throw e.fillInStackTrace(); // 18
}
}
}

Thanks for helping!!!



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us