Reputation: 3879
For an android library project, I'm trying to create a Force Close that would show a user of this library he has forgotten to do something.
To do so, I tried to create my custom exceptions, and throw
them, but what I only have is some warning in the LogCat, and nothing more.
Ex:
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)
Do you have any idea on how I could simulate/create this force close when this exception occurs?
Upvotes: 1
Views: 316
Reputation: 2785
try {
...
} catch (Exception e) {
//some code
}
This will catch all the exception your application made, because they are all descended from Exception, keep in mind that this is a very depreciated method
Upvotes: 0
Reputation: 36449
If you catch
an exception and print the stack trace, yes you get a "warning" and the app does not crash - that is the point of catching Exceptions. Make sure that MyCustomException
extends RuntimeException
(or use RuntimeException) and don't declare that isMenuButtonOk()
method throws
your exception.
For example:
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");
}
}
Upvotes: 3
Reputation: 3017
Display a Dialog
with a single button indicating Force Close and for body of that Button
put this 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();
Upvotes: 0