title: Android Studio - Exception locking surface cannot be caught
body: <p>Hi I made a game in Android Studio.  Occasionally when I close and restart the application on the emulator I get the following spammed into logcat:</p>
  
  <pre><code>Exception locking surface
  java.lang.IllegalStateException: Surface was already locked
  </code></pre>
  
  <p>I tried surrounding <code>canvas = this.surfaceHolder.lockCanvas();</code> with a try-catch block but the exception is not being caught.  is there a way I could check if the canvas is locked before attempting to lock it?</p>
  
  <p>Here is my code in the MainThread.java class</p>
  
  <pre><code>    while (running) {
          startTime = System.nanoTime();
          canvas = null;
  
      try {
       canvas = this.surfaceHolder.lockCanvas();
              synchronized (surfaceHolder) {
                  // update game state
                  this.gamePanel.update();
                  // draws the canvas on the panel
                  this.gamePanel.draw(canvas);
              }
  
          } catch(Exception e){e.printStackTrace();}finally {
              // in case of an exception the surface is not left in
              // an inconsistent state
              if (canvas != null) {
                  try {
  
                      surfaceHolder.unlockCanvasAndPost(canvas);
  
                  }
                  catch(Exception e){e.printStackTrace();}
              }
      }
  </code></pre>