• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is the usage of printStackTrace()?

 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the usage of printStackTrace()?May you give me example about it?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May give me example to show how does it work?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


java.lang.ArithmeticException
java.lang.Throwable()
java.lang.Exception()
java.lang.RuntimeException()
java.lang.ArithmeticException()
void ExceptionTest.main(java.lang.String [])
java.lang.ArithmeticException
java.lang.ArithmeticException / by zero


I did not understand at all what means in this quote!
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abalfazl hossein wrote:
java.lang.ArithmeticException
java.lang.Throwable()
java.lang.Exception()
java.lang.RuntimeException()
java.lang.ArithmeticException()
void ExceptionTest.main(java.lang.String [])
java.lang.ArithmeticException
java.lang.ArithmeticException / by zero


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
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12100
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic