title: Android - Catch RuntimeException during Instrumentation tests
body: <p>I am trying to set some instrumentation tests for some activities of a small Android application. I have successfully set some test classes, but I'm stuck with an issue : I can't seem to catch any Exceptions from my test methods.</p>
  
  <p>Currently, I have an Activity which requires some parameters to be set, and throws a <code>RuntimeException</code> if those parameters are not set.
  I have defined the following test class, to ensure that the Activity crashes if nothing is set :</p>
  
  <pre><code>public class DeviceWithoutMetadata extends ActivityInstrumentationTestCase2&lt;DeviceActivity_&gt;
  {
      public DeviceWithoutMetadata()
      {
          super(DeviceActivity_.class);
      }
  
      public void testCrashBecauseManifestPoorlyConfigured()
      {
          try
          {
              Logger.i("Beginning crash....");
              this.getActivity();
              Logger.i("Ending crash....");
          }
          catch (Throwable nmdre)
          {
              Logger.i("Absolutely normal to catch something here.");
          }
      }
  }
  </code></pre>
  
  <p>If I'm not mistaken, <code>this.getActivity()</code> launches the Activity, and that would cause a <code>RuntimeException</code> to be thrown (and I want my test to tell me "That's OK, I got a <code>RuntimeException</code>as expected"). The problem is that I can see my "Beginning crash" log, but neither "Ending crash" nor "Absolutely normal..." logs. Instead, my test fails by telling me the following (lighten) stack trace :</p>
  
  <pre><code>MonitoringInstrumentation: Exception encountered by: com.axible.axcore.activities.DeviceActivity_@305a0bd. Dumping thread state to outputs and pining for the fjords.
  com.axible.axcore.exception.runtime.NoMetadataDefinedRuntimeException: The calling activity must define a &lt;meta-data android:name="validation_scan_classname"&gt; tag, that must contain the name of the class which will validate the scan of the Device ID.
  at com.axible.axcore.activities.DeviceActivity.afterInject(DeviceActivity.java:36)
  at com.axible.axcore.activities.DeviceActivity_.init_(DeviceActivity_.java:48)
  at com.axible.axcore.activities.DeviceActivity_.onCreate(DeviceActivity_.java:38)
  at android.app.Activity.performCreate(Activity.java:6251)
  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
  </code></pre>
  
  <p>I tried to catch the specific exception (<code>NoMetadataDefinedRuntimeException</code>, which extends <code>RuntimeException</code>), then <code>RuntimeException</code>, then <code>Exception</code>, then <code>Throwable</code>... I can't catch anything.</p>
  
  <p>How can I catch this exception from my instrumentation tests ?</p>