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

前端 未结 5 1150
無奈伤痛
無奈伤痛 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:18

    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
    }
    

提交回复
热议问题