Java Android异常处理最佳实践?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16561692/
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
Android exception handling best practice?
提问by ldam
If my app crashes, it hangs for a couple of seconds before I'm told by Android that the app crashed and needs to close. So I was thinking of catching all exceptions in my app with a general:
如果我的应用程序崩溃,它会在 Android 告诉我应用程序崩溃并需要关闭之前挂起几秒钟。所以我想在我的应用程序中使用一般捕获所有异常:
try {
// ...
} catch(Exception e) {
// ...
}
And make a new Activity
that explains that the application crashed instantly (and also giving users an opportunity to send a mail with the error details), instead of having that delay thanks to Android. Are there better methods of accomplishing this or is this discouraged?
并创建一个新的Activity
解释应用程序立即崩溃(并且还让用户有机会发送包含错误详细信息的邮件),而不是由于 Android 而出现延迟。是否有更好的方法来实现这一点,还是不鼓励这样做?
Update:I am using a Nexus 5 with ART enabled and I am not noticing the delay I used to experience with apps crashing (the "hanging" I was talking about originally). I think since everything is native code now, the crash happens instantly along with getting all the crash information. Perhaps the Nexus 5 is just quick :) regardless, this may not be a worry in future releases of Android (given that ART is going to be the default runtime in Android L).
更新:我正在使用启用了 ART 的 Nexus 5,但我没有注意到我曾经遇到应用程序崩溃的延迟(我最初谈论的“挂起”)。我认为由于现在一切都是本机代码,崩溃会立即发生,同时获取所有崩溃信息。也许 Nexus 5 很快 :) 无论如何,这在 Android 的未来版本中可能不是问题(鉴于 ART 将成为 Android L 中的默认运行时)。
采纳答案by CRUSADER
Here, check for the link for reference.
在这里,检查链接以供参考。
In here you create a class say ExceptionHandlerthat implements java.lang.Thread.UncaughtExceptionHandler
..
在这里,你创建一个类发言权的ExceptionHandler说implements java.lang.Thread.UncaughtExceptionHandler
..
Inside this class you will do your life saving stuff like creating stacktrace and gettin ready to upload error report etc....
在本课程中,您将做一些挽救生命的事情,例如创建堆栈跟踪并准备好上传错误报告等......
Now comes the important parti.e. How to catch that exception.
Though it is very simple. Copy following line of code in your each Activity just after the call of super method in your overriden onCreate
method.
现在是重要的部分,即如何捕获该异常。虽然很简单。在覆盖onCreate
方法中调用 super 方法之后,在每个 Activity 中复制以下代码行。
Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
Your Activity may look something like this…
您的 Activity 可能看起来像这样……
public class ForceClose extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
setContentView(R.layout.main);
}
}
Hope this helps...
希望这可以帮助...
回答by Louis Evans
You could just use a generic alert dialog to quickly display error messages. For example...
您可以只使用通用警报对话框来快速显示错误消息。例如...
//******************************************
//some generic method
//******************************************
private void doStuff()
{
try
{
//do some stuff here
}
catch(Exception e)
{
messageBox("doStuff", e.getMessage());
}
}
//*********************************************************
//generic dialog, takes in the method name and error message
//*********************************************************
private void messageBox(String method, String message)
{
Log.d("EXCEPTION: " + method, message);
AlertDialog.Builder messageBox = new AlertDialog.Builder(this);
messageBox.setTitle(method);
messageBox.setMessage(message);
messageBox.setCancelable(false);
messageBox.setNeutralButton("OK", null);
messageBox.show();
}
You could also add other error handling options into this method, such as print stacktrace
您还可以在此方法中添加其他错误处理选项,例如打印堆栈跟踪
回答by sunyata
i found the "wtf" (what a terrible failure) method in the Log class. From the description:
我在 Log 类中找到了“wtf”(多么可怕的失败)方法。从描述来看:
Depending on system configuration, a report may be added to the DropBoxManager and/or the process may be terminated immediately with an error dialog.
根据系统配置,可能会向 DropBoxManager 添加报告和/或过程可能会立即终止并显示错误对话框。
http://developer.android.com/reference/android/util/Log.html
http://developer.android.com/reference/android/util/Log.html
Hope this helps someone
希望这有助于某人