1

I'm developing a small swing application, and I'm not sure if I should use the printStackTrace().

If I get an exception I show a user via JOptionPane a message, e.g: file not found etc.

But at the same time, I'm using the printStackTrace(), I wasn't sure about neither showing the stack trace to a user nor not to print anything...just in case it would be needed.

Can I leave the printStackTrace there or why not to?

thank you for advice.

Adeel Ansari
  • 39,541
  • 12
  • 93
  • 133
feiroox
  • 3,069
  • 8
  • 31
  • 31

4 Answers4

6

A better idea is to replace those with the use of any Logging API, like Log4J. And of course, as Paul mentioned, show the user meaningful error messages where ever appropriate.

Adeel Ansari
  • 39,541
  • 12
  • 93
  • 133
3

Log stack traces to a log file they wont mean anything to the end user anyway

Print meaningful error messages to users. i.e File not found etc

Paul Whelan
  • 16,574
  • 12
  • 50
  • 83
  • where I should place the file? – feiroox Apr 10 '09 at 08:43
  • I tend to log to /var/log/applicationname on linux systems. you can configure this via a properties file. So you don't need to harcode anything in your application. As Vinegar suggested using something like log4j or Java Logger http://java.sun.com/j2se/1.4.2/docs/api/java/util/logging/Logger.html – Paul Whelan Apr 10 '09 at 08:56
  • yes but that's the problem with files. /var/log/ doesn't have to be in windows and elsewhere. – feiroox Apr 10 '09 at 09:19
  • Have a look at the configuration part of the manual. http://logging.apache.org/log4j/1.2/manual.html – Adeel Ansari Apr 10 '09 at 09:43
  • You could if you wanted pass the logging location as a command line argument, default to the temp directory System.getProperty("java.io.tmpdir"); – Paul Whelan Apr 10 '09 at 09:45
1

printStackTrace() contains information relevant only for the developer so it is a good practice to avoid to expose them to the user

dfa
  • 114,442
  • 31
  • 189
  • 228
0

I agree that a logging framework is a good idea for any decently sized program. That being said most users are pretty comfortable with sending in a screen shot of any errors, so, from a support perspective, it can make life easier to include (a few) extra details in any error screens.

CurtainDog
  • 3,175
  • 21
  • 17
  • Here we have an automatic email feature which works like charm in sending the immediate error. But its safe to dump all in log files. – Adeel Ansari Apr 10 '09 at 09:48