61

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

} catch (Exception e) {
    e.printStackTrace();
}
Raedwald
  • 46,613
  • 43
  • 151
  • 237
jacknad
  • 13,483
  • 40
  • 124
  • 194

5 Answers5

49

I believe this is what you need:

catch (Exception e) {
     Log.e(TAG,Log.getStackTraceString(e)); 
}
sth
  • 222,467
  • 53
  • 283
  • 367
Adil Atilgan
  • 541
  • 4
  • 3
  • 48
    Nope. What the OP needs is `Log.e(TAG, "Explanation of what was being attempted when the exception was thrown", e)`. Note the third parameter. Log.e(String,String,Throwable) gets the stacktrace string from the Throwable for you. Use the `message` parameter for something meaningful. – spaaarky21 Jun 20 '14 at 23:27
  • @spaaarky21 You should make that an answer. – AndreKR May 05 '16 at 03:22
  • 2
    @AndreKR Someone already did. :) And I upvoted it. I just wanted to point out that this answer is promoting a bad practice, since it's rated so highly compared to correct answers. – spaaarky21 May 05 '16 at 16:54
  • @spaaarky21 I learned from your answer that I can pass an Exception to `android.util.Log.e()`. I didn't learn that from any of the other answers. (I didn't click the link in Nailuj's answer.) – AndreKR May 05 '16 at 16:56
  • @AndreKR Thanks. And good point. I was referring to Ryan's answer but now that I look again, I see that it's using `Log` in an unusual way – using a `Log` instance (I assume?) and passing the level in as a parameter. I added an answer. – spaaarky21 May 05 '16 at 18:09
46

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

Kewal Shah
  • 1,131
  • 18
  • 29
Julian
  • 20,008
  • 17
  • 77
  • 108
22

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.

spaaarky21
  • 6,524
  • 7
  • 52
  • 65
9

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
}
Tomas Narros
  • 13,390
  • 2
  • 40
  • 56
9

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.

palacsint
  • 28,416
  • 10
  • 82
  • 109
Ryan
  • 182
  • 2