Home » Android » java – Crash An App using Exception in Android

java – Crash An App using Exception in Android

Posted by: admin June 15, 2020 Leave a comment

Questions:

I just want to crash an Android application and display the “Dialog of Death” (Force Close Dialog) using Exception in Android. Is it possible? How is it done?

How to&Answers:

What you want is a RuntimeException, since the compiler will otherwise force you to move your code inside a try-catch block.

throw new RuntimeException("Crash!");

Answer:

I think you are looking for uncaughtExceptionHandler.

Here is a very good example,

http://iamvijayakumar.blogspot.in/2012/09/android-uncaughtexceptionhandler.html

use this code in your Activity to register a Uncaught Exception Handler,

Thread.setDefaultUncaughtExceptionHandler(new UnCaughtException(MainActivity.this));

Answer:

try to put any erroneous statement in try block, there are so many exceptions available like arithmetic ,arrayindexoutofbounds and so on then do what you want to do in catch.

And for displaying forceclose dialog, you need not to do something when you don’t catch it by default the force close dialog appears.

And why to catch the exception when you want to crash the app?