Java 将 printStackTrace() 用于 caugt Exceptions 是一个坏主意吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3855187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 05:53:46  来源:igfitidea点击:

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

javaexceptionprintstacktrace

提问by Hymannad

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

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

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

采纳答案by Julian

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

是的,这是个坏主意。您应该改用专为这些目的设计的 Android 内置日志类: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

Log.e(TAG, "message", e)消息可以是对抛出异常时所尝试内容的解释

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

或者只是Log.e(TAG, e)如果您不想为上下文提供任何消息

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

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

回答by Tomas Narros

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?

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

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
}

回答by Ryan

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

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

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

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

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

回答by Adil Atilgan

I believe this is what you need:

我相信这是你需要的:

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

回答by spaaarky21

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

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

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. Loghandles 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.

请注意,异常用作第三个参数,而不是附加到消息参数。Log为你处理细节——打印你的消息(它给出了你在代码中尝试做的事情的上下文)和Exception's 消息,以及它的堆栈跟踪。