使用 printStackTrace() 来处理异常是一个坏主意吗?

发布于 2024-09-26 11:30:17 字数 130 浏览 6 评论 0原文

在像这样的 Android 异常中使用 printStackTrace() 是一个坏主意吗?

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

同展鸳鸯锦 2024-10-03 11:30:17

我相信这就是您所需要的:

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

I believe this is what you need:

catch (Exception e) {
     Log.e(TAG,Log.getStackTraceString(e)); 
}
心房敞 2024-10-03 11:30:17

是的,这是一个坏主意。您应该使用专为这些目的而设计的 Android 内置日志类: http:// /developer.android.com/reference/android/util/Log.html

它为您提供了记录调试消息、警告、错误等的选项。

使用以下方式记录错误:

Log.e(TAG, "message" , e) 其中消息可以是对抛出异常时正在尝试执行的操作的解释

,或者如果您不想提供任何消息,则只需 Log.e(TAG, e)然后,

您可以在运行代码时单击底部的日志控制台,并使用标签或日志消息类型作为过滤器轻松搜索它

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

夜血缘 2024-10-03 11:30:17

是的。 printStackTrace() 很方便,但不鼓励,特别是在 Android 上,它可以通过 logcat 可见,但会以未指定的级别记录,并且没有正确的消息。相反,记录异常的正确方法是...

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

请注意,异常用作第三个参数,而不是附加到消息参数。 Log 为您处理详细信息 - 打印您的消息(给出您在代码中尝试执行的操作的上下文)和 Exception 的消息,以及它的堆栈跟踪。

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.

烟酉 2024-10-03 11:30:17

问题是:在 Andriod 应用程序上下文中打印堆栈跟踪是否有用?
标准输出在运行时是否可见?会有人关心吗?

我的观点是,如果没有人会检查标准输出并关心调试错误,则对此方法的调用就是死代码,并且编写堆栈跟踪消息是毫无价值的费用。如果您仅需要在开发时进行调试,则可以设置一个可访问的全局常量,并在运行时检查它:

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

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
}
迷爱 2024-10-03 11:30:17

我会避免使用 printStackTrace(),使用日志系统及其对异常的支持。

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

因此,如果您想更改日志记录的处理方式,那就容易多了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文