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

前端 未结 5 1152
無奈伤痛
無奈伤痛 2020-12-02 16:54

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

} catch (Exception e) {
    e.printStackTrace();
}
5条回答
  •  醉酒成梦
    2020-12-02 17:17

    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.

提交回复
热议问题