aspose file tools*
The moose likes Beginning Java and the fly likes what is the usage of printStackTrace()? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what is the usage of printStackTrace()?" Watch "what is the usage of printStackTrace()?" New topic
Author

what is the usage of printStackTrace()?

abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 606
what is the usage of printStackTrace()?May you give me example about it?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

e.printStackTrace() // where "e" is the exception.

It prints the stack trace: the execution path of the program up until the exception.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 606
May give me example to show how does it work?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I don't know what kind of example you're looking for. It seems somewhat self-explanatory to me, and really easy to try on your own.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 606




I did not understand at all what means in this quote!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
abalfazl hossein wrote:
Not, it won't do anything with I/O errors.
This class will do several different things if run from the command line
campbell@****-laptop:~/java$ java ExceptionDemo
. . .
campbell@****-laptop:~/java/$ java ExceptionDemo Campbell
. . .
campbell@****-laptop:~/java$ java ExceptionDemo 123
. . .
Each of those executions will produce a different sort of Exception, and you can see the stack traces at the command line. They tell you where the Exception originated, and all the lines it has passed through until it was caught. If yoo delete the catch block and the keyword try, you can repeat the exercise and see whether you obtain a different stack trace.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
abalfazl hossein wrote:


I did not understand at all what means in this quote!
You are printing the stack trace, as well as the toString() representation of the Exception. Since you are using System out (don't use System.out for Exceptions, use System.err), you have part printed on System.out and part (the stack trace) on System.err, and you may actually have the lines from one print-out interspersed with the other.
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

Campbell Ritchie wrote:You are printing the stack trace, as well as the toString() representation of the Exception. Since you are using System out (don't use System.out for Exceptions, use System.err), you have part printed on System.out and part (the stack trace) on System.err, and you may actually have the lines from one print-out interspersed with the other.


Does that mean, System.err() wont give the toString() representation?!
Is there any difference at all between the 2 - System.out() and System.err()?? Both prints into the monitor,right?


OCPJP 6
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

They do both print to the monitor but they print in different streams which means that the values they are printing could potentially overlap. When printing out error information you should use System.err.println() like Campbell suggested, This will guarantee that your data won't overlap.

Hunter


"If the facts don't fit the theory, get new facts" --Albert Einstein
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
As Hunter McMillan implies, you can get your data printed in different orders if you use System.out and System.err simultaneously in repeated executions. You do actually appear to have overlapping outputs on what you quoted.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10905
    
  12

System.out and System.err both print to the screen by default, but don't have to both print there. You can redirect them to different files.

If you do have both print to the screen, it's effectively (perhaps literally) two different threads doing the printing. So, you may get 2 lines from one, then a line from the other, then 3 more from the first, then 8 from the second, etc.




There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what is the usage of printStackTrace()?
 
Similar Threads
instance in a for loop - right /wrong/doesn't matter ?
iPod Hold button?
Use of printStackTrace
Java mail problem in IE
Interface as a parameter