I read here that e.printStackTrace()
should not be used to catch exceptions on android.
我在这里看到,不应该使用e.printStackTrace()来捕获android上的异常。
On another source I read that I should use Log.e()
instead of e.printStackTrace()
:
在另一篇文章中,我看到我应该使用Log.e()而不是e.printStackTrace():
... catch (Exception e) {
Log.e(TAG, "Foo did not work", e);
}
Do I have to manually remove these calls in the release build? What is the best practice to trace exception logs in Android?
我是否必须在发布版本中手动删除这些调用?在Android中跟踪异常日志的最佳实践是什么?
1 个解决方案
#1
2
The main intent of catching exceptions, is of course handling an occuring problem and due to this your catch part should always handle the error in some way e.g. use default values for the things which did not work, or try something different.
捕获异常的主要目的当然是处理正在发生的问题,因此您的catch部分应该始终以某种方式处理错误,例如,对不能工作的事情使用默认值,或者尝试其他方法。
Anyway, if you are planning to publish your app in the Google Play Store and if you want to log the exceptions and use them for debugging puropses, to further improve app you can use Google Analytics API part for handling exceptions. It also provides facilities for collecting stack traces. (this is an exmaple from the API pages)
无论如何,如果您打算在谷歌Play Store中发布您的应用程序,如果您想要记录异常并将其用于调试purops,为了进一步改进应用程序,您可以使用谷歌Analytics API部分来处理异常。它还提供了收集堆栈跟踪的工具。(这是来自API页面的exmaple)
// Using StandardExceptionParser to get an Exception description.
try {
// Request some scores from the network.
ArrayList<Integer> highScores = getHighScoresFromCloud();
} catch (IOException e) {
// Get tracker.
Tracker t = ((AnalyticsSampleApp) getActivity().getApplication()).getTracker(
TrackerName.APP_TRACKER);
t.send(new HitBuilders.ExceptionBuilder()
.setDescription(
new StandardExceptionParser(this, null)
.getDescription(Thread.currentThread().getName(), e))
.setFatal(false)
.build()
);
... // Display alert to user that high scores are currently unavailable.
}
Afterwards all tracked stracktraces will be display in the Google Developer Console in the tap of you app.
之后,所有被跟踪的stracktrace都将显示在谷歌开发人员控制台的您的应用。
You can also add an Listener for catching unhandled exceptions. So whenever an exceptions occours which you did not expect whis handler will be invoked.
您还可以添加侦听器来捕获未处理的异常。因此,每当您没有预料到的异常occours将被调用。
Thread.setDefaultUncaughtExceptionHandler(new MyReportHelper());
Of course you should always try to catch and handle all exceptions which you know of, this handler is only for emergencies. For more details how an handler could look like see here.
当然,您应该始终尝试捕获和处理您所知道的所有异常,此处理程序仅用于紧急情况。有关处理程序的详细信息,请参见这里。
#1
2
The main intent of catching exceptions, is of course handling an occuring problem and due to this your catch part should always handle the error in some way e.g. use default values for the things which did not work, or try something different.
捕获异常的主要目的当然是处理正在发生的问题,因此您的catch部分应该始终以某种方式处理错误,例如,对不能工作的事情使用默认值,或者尝试其他方法。
Anyway, if you are planning to publish your app in the Google Play Store and if you want to log the exceptions and use them for debugging puropses, to further improve app you can use Google Analytics API part for handling exceptions. It also provides facilities for collecting stack traces. (this is an exmaple from the API pages)
无论如何,如果您打算在谷歌Play Store中发布您的应用程序,如果您想要记录异常并将其用于调试purops,为了进一步改进应用程序,您可以使用谷歌Analytics API部分来处理异常。它还提供了收集堆栈跟踪的工具。(这是来自API页面的exmaple)
// Using StandardExceptionParser to get an Exception description.
try {
// Request some scores from the network.
ArrayList<Integer> highScores = getHighScoresFromCloud();
} catch (IOException e) {
// Get tracker.
Tracker t = ((AnalyticsSampleApp) getActivity().getApplication()).getTracker(
TrackerName.APP_TRACKER);
t.send(new HitBuilders.ExceptionBuilder()
.setDescription(
new StandardExceptionParser(this, null)
.getDescription(Thread.currentThread().getName(), e))
.setFatal(false)
.build()
);
... // Display alert to user that high scores are currently unavailable.
}
Afterwards all tracked stracktraces will be display in the Google Developer Console in the tap of you app.
之后,所有被跟踪的stracktrace都将显示在谷歌开发人员控制台的您的应用。
You can also add an Listener for catching unhandled exceptions. So whenever an exceptions occours which you did not expect whis handler will be invoked.
您还可以添加侦听器来捕获未处理的异常。因此,每当您没有预料到的异常occours将被调用。
Thread.setDefaultUncaughtExceptionHandler(new MyReportHelper());
Of course you should always try to catch and handle all exceptions which you know of, this handler is only for emergencies. For more details how an handler could look like see here.
当然,您应该始终尝试捕获和处理您所知道的所有异常,此处理程序仅用于紧急情况。有关处理程序的详细信息,请参见这里。