• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

what is the usage of printStackTrace()?

 
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the usage of printStackTrace()?May you give me example about it?
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
May give me example to show how does it work?
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.



 
And tomorrow is the circus! We can go to the circus! I love the circus! We can take this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic