• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

why is printStackTrace() not recommended ?

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there ,
Whenever I write the following kind of code, there is a warning in Netbeans that it is not recommended.


Why netbeans suggests that Throwable.printStackTrace() should be removed. ?
Any suggestions will be appreciated..
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe because it would be better to use a logging framework to capture it? I use IntelliJ, and when it has warnings like that, you can mouseover or something to get a more detailed explanation. You might see if NB offers something similar. Ultimately though, it will be a matter of style. I would also suggest that it's better to use a logger rather than System.out and System.err, but you or your instructor/boss/team will have to decide whether you need that.
 
Marshal
Posts: 76462
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Probably because printStackTrace() is so very basic. It displays information which is useful for development, but the user will have an incomprehensible printout, which will simply be forgotten.
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I use printStackTrace() with regularity. I don't use a logger like Log4j (often - it's great to use if you need it though) because the only time anyone should see errors relevant to stack traces are in development and they are very useful to me. Of course, I also return some message to the user in production so that the UI can inform the user of what's going on. In a production environment, however, no one should ever see a stack trace and if a stack trace is printing, it's probably indicating that I have an issue I need to fix in the code.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alvin Watkins wrote:the only time anyone should see errors relevant to stack traces are in development



Stack traces are also a useful diagnostic tool in production. The end user shouldn't see them of course, but they can be invaluable to the developer for tracking down problems that are hard to reproduce in a dev environment.
 
Raman Ghai
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:You might see if NB offers something similar.


Netbeans writes a logger statement by default. May be this is the best method to read exceptions so that the end user doesn't get to know that some problems even occurred.
Thanks everyone for your invaluable comments.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raman Ghai wrote:Hi there ,
Why netbeans suggests that Throwable.printStackTrace() should be removed. ?
Any suggestions will be appreciated..



Probably because in any real world system it's quite unlikely that you'd want errors to be simply dumped to the stderr stream. You want the flexibility that a logging framework provides to save, route and act on errors in a way that ensures they come to the attention of the right people. Of course, in any large production system you probably have a whole custom exception management component anyway, but still...
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raman Ghai wrote:Hi there ,
Whenever I write the following kind of code, there is a warning in Netbeans that it is not recommended.


Why netbeans suggests that Throwable.printStackTrace() should be removed. ?
Any suggestions will be appreciated..



It is not recommended because you only catch an exception and print stack trace onto stderr, without handling the exception (after the catch block is completed the code will continue to execute even if hard error occurred and that is most likely not what the intention was
 
Campbell Ritchie
Marshal
Posts: 76462
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
No prison can hold Chairface Chippendale. And on a totally different topic ... my stuff:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic