Click to See Complete Forum and Search --> : CallStack


slepax
August 21st, 2000, 06:41 AM
Hey all,

I want to get the name of the method which is previous to the method I am currently in (i.e. the method which called my current method).

The best will be to get a CallStack list, but.. how ?
(I could only found printCallStack, and it doesn't really help...)


Thanks alot,
Ronen.

mbauer
August 21st, 2000, 08:16 AM
Ok, I checked Runtime and System, but niether have a callStack() method. I know I saw one somewhere, but can't exactly remember where it was.

Anyway, here is a workaround. Wherever you want to do a call stack, throw and catch an Exception. All Exception classes have printStackTrace(), printStackTrace(PrintStream s), and printStackTrace(PrintWriter s) methods which will not only print a stack trace, but also allows you to redirect that to any output stream you want. You could always pipe this into a reader that analyses the stack trace.

Example:


try {
throw new Exception("Stack trace!");
} catch(Exception e) {
e.printStackTrace(myOutputStream);
}



Hope this gets you somewhere,


Mike Bauer

Michael Bauer

vlasov pavel
October 24th, 2000, 11:13 AM
I see it could be made simplier:

new Exception("stack trace").printStackTrace();




And class java.lang.Thread has support on this:

// in any point of code you can dump stack on stdout(or stdin ?)
Thread.dumpStack();