I want to test out crash report using acra but the first step is I need to simulate a fatal crash in Android using code.
Any idea?
in addition to @vinnet-shukla answer:
"OR simply throw an uncaught exception"
throwing uncaught exception to do a crash is bad idea as the exception could by caught somwhere higher in the stack - especially when whe don't know where we are right now :)
more ellegant way is to use ERROR
An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. The ThreadDeath error, though a "normal" condition, is also a subclass of Error because most applications should not try to catch it.
so we could make our own Error subclass and use it:
/*
* usage:
*
* CrashError.doCrash();
*
*/
public class CrashError extends Error {
public CrashError() {
this("simulated crash");
}
public CrashError(String msg) {
super(msg);
}
public static void doCrash() {
throw new CrashError();
}
}
but if we talk about other possibilities we could also a throw checked exception :)
this will also be a lesson how we could RETHROW CHECKED EXCEPTION :) with other way than use of sun.misc.Unsafe especially when the class is not available in VM implementation
@SuppressWarnings("unchecked")
public static <E extends Throwable> void throwAnyT(Throwable e) throws E {
throw (E) e;
}
public static void throwUnchecked(Throwable e) {
throwAny(e);
// should never get there.
throw new InternalError();
}
public static void crash() {
throwUnchecked(new java.io.IOException("simulated crash"));
}
in addition to @audric answer:
"You can't throw null"
yes you can :) it's exactly what you are doing in your example and yes it could get undetectable if the catch block will not use Throwable - the NPX will never be thrown and simply there will be normal flow when code will still execute and yes i'll have seen this and experienced by myself :) on ANDROID DALVIK
and what about..? maybe it could fulfill your needs?
java specific (also in android):
- Runtime.getRuntime().exit(int);
- System.exit(int);
- Runtime.getRuntime().halt(int);
android specific:
- android.os.Process.killProcess(int);
- android.os.Process.killProcessQuiet(int);
- android.os.Process.sendSignal(int,int);
- android.system.Os.kill(int);
Use the following code:
String xyz=null;
system.out.println(xyz);
A very simple approach... and is very important to understand that why it happened.
Try initiating a variable in onCreate()
before the setContentView()
method, then use it to call a method or variable or try registering it to some listener..
Eg:
Button b;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
b = (Button)findViewById(R.id.butt);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
}
});
setContentView(R.layout.main);
}
This crashed, because before setContentView()
none of the components/view in the main.xml layout got their ids.
Simply create in your main.xml a button like this:
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="crash"
android:text="Crash me" />
then run your app and click for crash
If you are using firebase crashlytics, then there is a very easy way to do this. Mentioned in their document also.
val crashlytics = FirebaseCrashlytics.getInstance()
crashlytics.log("my message")
you can crash with a simple null point exception.
throw new NullPointerException();