title: Why does not the try/catch in java work on my case?
body: <p>I have written a try/catch block on java selenium and I do not understand why it does not function. It is supposed to check for a web element on the webpage and if it does not find it should throw a NoSuchElementException and go to the catch block.What really happens is that the element is not found and the execution does not continue to catch block,it fails the test(Selenium WebTest) and stops the execution completely,failing my test.I have also included &quot;throws NoSuchElementException&quot; in the main method. Here is some code for reference. What can the problem be? How can I write a failproof code?</p>
  <pre><code> try {
  // this is the web element to be found by xpath,sometimes it is on the webpage,sometimes not
  
  can = driver.findElement(By.xpath(&quot;//*[@id=\&quot;revealGameAnimation\&quot;]/canvas&quot;));
  }
  
  catch(NoSuchElementException e)
  {
  
  // some code here
  
  }
  </code></pre>