title: Cannot catch NoSuchElementException
body: <p>I've seen quite a few threads on this topic, however I didn't find an answer for my question. I managed to solve my general issue, but I'd like to understand why my first attempts didn't work.</p>
  <p>I have a class that is supposed to wait until an element with a specified class appears.</p>
  <p>Here's the original code:</p>
  <pre class="lang-cs prettyprint-override"><code>void Wait(string className)
  {
      var wait = new WebDriverWait(driver, timeout: TimeSpan.FromSeconds(30))
      {
          PollingInterval = TimeSpan.FromSeconds(2)
      };
      wait.IgnoreExceptionTypes(typeof(NoSuchElementException));
      wait.Until(drv =&gt; drv.FindElement(By.ClassName(className)));
  }
  </code></pre>
  <p>Even though I used <code>wait.IgnoreExceptionTypes(typeof(NoSuchElementException));</code>, the <code>NoSuchElementException</code> exception is being thrown anyway on the line <code>wait.Until(drv =&gt; drv.FindElement(By.ClassName(className)));</code>.</p>
  <p>My second attempt was this:</p>
  <pre class="lang-cs prettyprint-override"><code>void Wait(string className)
  {
      var wait = new WebDriverWait(driver, timeout: TimeSpan.FromSeconds(30))
      {
          PollingInterval = TimeSpan.FromSeconds(2)
      };
      wait.IgnoreExceptionTypes(typeof(NoSuchElementException));
      
      try
      {
          wait.Until(drv =&gt; drv.FindElement(By.ClassName(className)));
      }
      catch(NoSuchElementException) {}
  }
  </code></pre>
  <p>Again, the same line throws the exception, even though it's inside of <code>try-catch</code>. Here's a screenshot:</p>
  <p><a href="https://i.stack.imgur.com/FvzDM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FvzDM.png" alt="enter image description here" /></a></p>
  <p>My final code looks as follows and it works:</p>
  <pre class="lang-cs prettyprint-override"><code>void Wait(string className)
  {
      var wait = new WebDriverWait(driver, timeout: TimeSpan.FromSeconds(30))
      {
          PollingInterval = TimeSpan.FromSeconds(2)
      };
      wait.IgnoreExceptionTypes(typeof(NoSuchElementException));
      
      wait.Until(drv =&gt;
      {
          try
          {
              return drv.FindElement(By.ClassName(className));
          }
          catch(NoSuchElementException) { }
  
          return null;
      });
  }
  </code></pre>
  <p>I'm catching an exception inside of the lambda function.</p>
  <p>The things that I don't understand:</p>
  <ol>
  <li>In the first approach, why the <code>IgnoreExceptionTypes</code> method doesn't work?</li>
  <li>In the second approach, why isn't the exception caught? Doens't it &quot;bubble up&quot; from lambda where it's originally thrown?</li>
  </ol>