Question

I am interested in how exceptions in Android work when trown manually. I tried to make an app where you can push a button and a random exception would popup. Here I noticed something strange. When for instance, I throw an ArrayIndexOutOfBoundsException, I get an IlligalStateException.

Function that generates exception:

public void GenerateRandomException(View view)
{
    Random rand = new Random();
    int random = rand.nextInt(5);

    switch(random)
    {
        case 0:
            throw new ArrayIndexOutOfBoundsException("An ArrayIndexOutOfBoundsException occured!");
        case 1:
            throw new RuntimeException("A RuntimeException occured!");
        case 2:
            throw new NullPointerException("An NullPointerException occured!");
        case 3:
            throw new IllegalThreadStateException("An IllegalThreadStateException occured!");
        case 4:
            throw new IllegalArgumentException("An IllegalArgumentException occured!");
        default:
            break;
    }
}

Stacktrace:

FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
    at android.view.View$1.onClick(View.java:3838)
    at android.view.View.performClick(View.java:4475)
    at android.view.View$PerformClick.run(View.java:18786)
    at android.os.Handler.handleCallback(Handler.java:730)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:176)
    at android.app.ActivityThread.main(ActivityThread.java:5419)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at android.view.View$1.onClick(View.java:3833)
    ... 11 more
Caused by: java.lang.ArrayIndexOutOfBoundsException: An ArrayIndexOutOfBoundsException occured!
    at gamebugtracker.lib.MainActivity.GenerateRandomException(MainActivity.java:78)
    ... 14 more

As seen in the stacktrace, it says there is an IlligalStateEception, caused by the ArrayIndexOutOfBoundsException.
I find this strange, shouldn't the ArrayIndexOutOfBoundsException, be the top exception? Or did I broke my own code?

Was it helpful?

Solution

As you can see, your ArrayIndexOutOfBounds is the bottommost "caused by" exception in the stack trace.

There are other catch blocks in between that catch your exception and wrap it to another exception using Exception(String,Throwable) constructor. In this case this wrapper code is in the code that transforms your XML onClick declaration to a OnClickListener, with the intermediate InvocationTargetException coming from Java reflection.

Ultimately the exception gets caught by the top-level exception handler that logs it as "FATAL EXCEPTION" and terminates your app. The log output includes all nested exceptions as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top