Is it a bad idea to use printStackTrace() for caugt Exceptions?

JavaExceptionPrintstacktrace

Java Problem Overview


Is it a bad idea to use printStackTrace() in Android Exceptions like this?

} catch (Exception e) {
	e.printStackTrace();
}

Java Solutions


Solution 1 - Java

I believe this is what you need:

catch (Exception e) {
     Log.e(TAG,Log.getStackTraceString(e));	
}

Solution 2 - Java

Yes, it is a bad idea. You should instead use Android's built-in log class specifically designed for these purposes: http://developer.android.com/reference/android/util/Log.html

It gives you options to log debug messages, warnings, errors etc.

Logging errors with:

Log.e(TAG, "message", e) where the message can be an explanation of what was being attempted when the exception was thrown

or simply Log.e(TAG, e) if you do not wish to provide any message for context

You can then click on the log console at the bottom while running your code and easily search it using the TAG or log message type as a filter

Solution 3 - Java

Yes. printStackTrace() is convenient but discouraged, especially on Android where it is visible through logcat but gets logged at an unspecified level and without a proper message. Instead, the proper way to log an exception is...

Log.e(TAG, "Explanation of what was being attempted", e);

Note that the exception is used as a third parameter, not appended to the message parameter. Log handles the details for you – printing your message (which gives the context of what you were trying to do in your code) and the Exception's message, as well as its stack trace.

Solution 4 - Java

I would avoid using printStackTrace(), use a logging system and its support of exceptions.

log.log(Level.SEVERE, "Uncaught exception", e);

So if you want to change how logging is handled it's much easier.

Solution 5 - Java

The question is: is useful at all print to the stack trace in an Andriod application context? Will the standard output be visible at runtime? Will somebody care about it?

My point is that, if nobody is going to check the standard output and care to debug the error, the call to this method is dead code, and composing the stacktrace message is a worthless expense. If you need it only for debugging at development, you could set an accesible global constant, and check it at runtime:

} catch (Exception e) {
   if(com.foo.MyEnvironmentConstants.isDebugging()) {
      e.printStackTrace();
   } //else do noting
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjacknadView Question on Stackoverflow
Solution 1 - JavaAdil AtilganView Answer on Stackoverflow
Solution 2 - JavaJulianView Answer on Stackoverflow
Solution 3 - Javaspaaarky21View Answer on Stackoverflow
Solution 4 - JavaRyanView Answer on Stackoverflow
Solution 5 - JavaTomas NarrosView Answer on Stackoverflow