Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
Well, it's always been possible to just say "new Exception().printStackTrace()." A lot of people are surprised about that. The object doesn't need to be thrown to contain a useful stack trace.
But in recent Java versions, you can call dumpStack() on a Thread to print a trace, and you can also call Thread.getStackTrace() to get an array of StackTraceElement objects, which describe each stack frame nicely. Use the static method Thread.currentThread() to get the currently executing Thread.
The stack trace you are printing here is of the new Exception object you have just created. If you want to print the stack trace of the caught exception you need
Because the new exception you created in the catch block was a java.lang.Exception. If you wanted it to be an ArithmeticException then you would need to have written
However, you don't need to create a new exception to print the stack trace. You already have an ArithmeticException (assuming that is what is thrown when you divide by 0). Although you are catching an Exception, the type of the object referenced by e will be ArithmeticException. Try changing your code to this
Sunil, the original question in this thread was about how to get the stack trace without an exception object. If you do already have an exception (e.g. if you're in a catch block), you should ignore this thread - except Joanne's last post. Just use the stack track from the existing exception.