发生自定义异常时创建强制关闭

时间:2013-08-02 16:38:54

标签: android exception

对于一个Android库项目,我正在尝试创建一个Force Close,它会向该库的用户显示他忘记做某事。

为此,我尝试创建自定义异常,并throw,但我只有一些警告在LogCat中,仅此而已。

例如:

MyActivity.java

protected void onResume() {
    super.onResume();
    try {
        this.isMenuButtonOk();
    } catch(MyCustomException e) {
        e.printStackTrace();
    }
}
private void isMenuButtonOk() throws MyCustomException{
    if(!this.mOnCreateOptionsMenuHasBeenCalledFlag)
        throw new MyCustomException("OnCreateOptionMenu() needs to call it's parent. Or you need to deactivate the use of the menubutton");
}

logcat的

08-02 18:28:13.507    3866-3866/com.example.testlibrary W/System.err: com.example.testlibrary.MyCustomException: OnCreateOptionMenu() needs to call it's parent. Or you need to deactivate the use of the menubutton
08-02 18:28:13.511    3866-3866/com.example.testlibrary W/System.err: at com.example.testlibrary.lib.MyActivity.isMenuButtonOk(MyActivity.java:286)
08-02 18:28:13.511    3866-3866/com.example.testlibrary W/System.err: at com.example.testlibrary.lib.MyActivity.onResume(MyActivity.java:244)
08-02 18:28:13.511    3866-3866/com.example.testlibrary W/System.err: at com.example.testlibrary.MainActivity.onResume(MainActivity.java:304)
08-02 18:28:13.511    3866-3866/com.example.testlibrary W/System.err: at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1184)
08-02 18:28:13.511    3866-3866/com.example.testlibrary W/System.err: at android.app.Activity.performResume(Activity.java:5082)
08-02 18:28:13.511    3866-3866/com.example.testlibrary W/System.err: at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2565)
08-02 18:28:13.511    3866-3866/com.example.testlibrary W/System.err: at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2603)
08-02 18:28:13.511    3866-3866/com.example.testlibrary W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2089)
08-02 18:28:13.511    3866-3866/com.example.testlibrary W/System.err: at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-02 18:28:13.511    3866-3866/com.example.testlibrary W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-02 18:28:13.511    3866-3866/com.example.testlibrary W/System.err: at android.os.Handler.dispatchMessage(Handler.java:99)
08-02 18:28:13.511    3866-3866/com.example.testlibrary W/System.err: at android.os.Looper.loop(Looper.java:137)
08-02 18:28:13.511    3866-3866/com.example.testlibrary W/System.err: at android.app.ActivityThread.main(ActivityThread.java:4745)
08-02 18:28:13.511    3866-3866/com.example.testlibrary W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
08-02 18:28:13.511    3866-3866/com.example.testlibrary W/System.err: at java.lang.reflect.Method.invoke(Method.java:511)
08-02 18:28:13.511    3866-3866/com.example.testlibrary W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-02 18:28:13.515    3866-3866/com.example.testlibrary W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-02 18:28:13.515    3866-3866/com.example.testlibrary W/System.err: at dalvik.system.NativeStart.main(Native Method)

您是否知道如何在发生此异常时模拟/创建此力量?

3 个答案:

答案 0 :(得分:3)

如果您catch出现异常并打印堆栈跟踪,是的,您会收到“警告”并且应用程序不会崩溃 - 这是捕获异常的重点。确保MyCustomException扩展RuntimeException(或使用RuntimeException),并且不要将isMenuButtonOk()方法throws声明为您的例外。

例如:

protected void onResume() {
    super.onResume();
    this.isMenuButtonOk();
}

private void isMenuButtonOk(){
    if(!this.mOnCreateOptionsMenuHasBeenCalledFlag){
        throw new RuntimeException("OnCreateOptionMenu() needs to call it's parent. Or you need to deactivate the use of the menubutton");
    }
}

答案 1 :(得分:0)

使用单个按钮显示Dialog,表示强制关闭Button的正文显示System.exit(1);

AlertDialog alertDialog = new AlertDialog.Builder(getApplicationContext()).create();

alertDialog.setTitle("An error occured");

alertDialog.setMessage(<your-message>);

alertDialog.setButton("Force Close", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
             System.exit(1);
         }
    });

alertDialog.show();

答案 2 :(得分:0)

try {
     ...
} catch (Exception e) {
  //some code
}

这将捕获您的应用程序所做的所有异常,因为它们都是Exception的后代,请记住这是一个非常折旧的方法