This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
Probably, I did not get what exactly you sought, but for getting stack trace of main thread when an exception occured, you can use following code sample:
how can I get the stack trace of the above method calls in method3()
By printing statements, as you did, in each method, you can trace the flow of method calls. Stack Trace is like information about methods and statements when some exception occurs, and helps you find out the root cause as you navigate to it looking at the information.
If you just want to dump the current stack trace to the console you can use the Thread class' dumpStack() method which outputs the stack trace to stderr. If you want your code to have access to the stack trace, the Thread class has methods for this too.
Joined: Jun 14, 2010
According to the Threads chapter in the SCJP 5 book
"The main() method, that starts the whole ball rolling, runs in one thread, called
(surprisingly) the main thread. If you looked at the main call stack (and you can, any
time you get a stack trace from something that happens after main begins, but not
within another thread), you'd see that main() is the first method on the stack—
the method at the bottom."
So , all I want is just a stack trace which highlights the order of method calls
in the main thread which as in this code has no exception code at all.
How can this be achieved ?
Joined: Aug 07, 2007
Rajiv Rai wrote:How can this be achieved ?
I've already told you, you call the Thread class' dumpStack() method. This will print out the stack trace to the console for the thread that calls the method. ie
If myMethod() is called from the main thread it will print the stack trace for all method calls back to main().
If you want something other than this can you give more details of what you want to do.