title: How to catch exceptions in JUnit
body: <p>My JUnit test does not catch the exception because of my return statement in the catch block. When I delete returning statement, the test passes. I want my unit test to work with returning statement if the exception occurs.</p>
  
  <p>I've also tried JUnit 5 stuff but it does not fix the problem.</p>
  
  <p>My method:</p>
  
  <pre><code>public ArrayList&lt;Action&gt; parsePlaByPlayTable() {
      ArrayList&lt;Action&gt; actions = new ArrayList&lt;&gt;();
      Document document = null;
  
      try {
        document = Jsoup.connect(url).get();
      } catch (Exception e) {
        log.error(e.getMessage());
        return new ArrayList&lt;&gt;();
      }
  
      // if the exception occurs and there is no return in the catch block,
      // nullPointerException is thrown here
      Element table = document.getElementById("pbp"); 
  
      // more code. . .
  }
  </code></pre>
  
  <p>My test:</p>
  
  <pre><code>  @Test(expected = Exception.class)
    public void testParsePlaByPlayTableInvalidUrl() {
      PlayByPlayActionHtmlParser parser = new PlayByPlayActionHtmlParser("https://www.basketbal-reference.com/oxscores/pbp/201905160GS.html");
      ArrayList&lt;Action&gt; actions = parser.parsePlaByPlayTable();
    }
  </code></pre>