title: Uncaught Exception Handler - UiAutomator
body: <p>I would like to have UiAutomator take a screenshot when a test fails. The way I currently have this set up is like so:</p>
  
  <pre><code>    @Override
  protected void setUp() throws Exception {
      System.out.println("Beginning setup...");
      testFailed = true;
  }
  
  @Override
  protected void tearDown() throws Exception {
      if(testFailed)
          getUiDevice().takeScreenshot("/sdcard/Pictures/Screenshots/failure.jpg");
  }
  </code></pre>
  
  <p>then the individual test works like this</p>
  
  <pre><code>public void testExample(){
    /* do stuff */
    testFailed = false;
  }
  </code></pre>
  
  <p>the idea is that if an exception is thrown, testFailed is never set to false, and tearDown take a screenshot when it is called after the test.</p>
  
  <p>I was really hoping to be able to add a testListener, or uncaught exception handler into my code for a more elegant solution to this problem. However, UiAutomator has its own uncaught exception handler that overrides the one I tried to implement. I can add my uncaught exception handler but it's just ignored.</p>
  
  <p>Is there any way that I can add an uncaught exception handler, that will intercept exceptions between where they are thrown and UiAutomator's uncaught exception handler, then pass them along (to UiAutomator)?</p>
  
  <p>Is there another way I can listen for a test failing / an exception being thrown without specifically adding that Boolean into the end of every test?</p>