Wednesday, April 20, 2011

Is it good to use printStackTrace() in swing program?

Hi, 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.

From stackoverflow
  • 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

    feiroox : where I should place the file?
    Paul Whelan : 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
    feiroox : yes but that's the problem with files. /var/log/ doesn't have to be in windows and elsewhere.
    Adeel Ansari : Have a look at the configuration part of the manual. http://logging.apache.org/log4j/1.2/manual.html
    Paul Whelan : You could if you wanted pass the logging location as a command line argument, default to the temp directory System.getProperty("java.io.tmpdir");
  • printStackTrace() contains information relevant only for the developer so it is a good practice to avoid to expose them to the user

  • 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.

  • 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.

    Adeel Ansari : 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.

0 comments:

Post a Comment