• Post Reply Bookmark Topic Watch Topic
  • New Topic

StackTrace  RSS feed

 
Arun Kumar
Ranch Hand
Posts: 135
Chrome Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any way to retrieve the StackTrace with out an exception object
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Arun Kumar
Ranch Hand
Posts: 135
Chrome Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ernest Friedman-Hill
 
suneel kumar
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class FinallyBlock {
public static void main(String args[]){
try {

System.out.println("before exception");

int i= 10/0;


}
catch(Exception e){
System.out.println("with in the catch block");
new Exception().printStackTrace();

}
finally{
System.out.println("With in the finally Block");

}
}
}

this code has to display the ArithmeticException
but i am getting java.lang.Exception.
Why
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
suneel kumar
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so , if we create a new exception in the catch block why con't it gives
ArithmeticException...?
can you explaine me clearly ?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
suneel kumar
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot , i got the concept
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!