title: How to wrap exceptions handling in helper class with lambda expressions
body: <p>I am having troubles while trying to refactor exception handling logic in an helper class.</p>
  
  <p>My code uses a repository which accesses a database and might throw the custom exception <code>RepositoryException</code>. If such exception is thrown by the repository, I want my code to catch it and set an error label in the graphical user interface (<code>view</code>):</p>
  
  <pre class="lang-java prettyprint-override"><code>... // more code
  try {
      existingCourse = repository.findByTitle(course.getTitle());  // &lt;- throws RepositoryException
  } catch (RepositoryException e) {
      view.showError(e.getMessage(), course);
      return;
  }
  ... // some more code
  </code></pre>
  
  <p>The point is that this code is repeated several times and I would prefer to have it refactored in an helper class. 
  This is what I came up to after some experiments:</p>
  
  <ul>
  <li>A custom <code>FunctionalInterface</code> called <code>ThrowingSupplier</code>, which represent the code that throws the exception.</li>
  <li>A <code>TransactionManager</code> helper class, with a <code>catcher</code> methods that accepts a <code>ThrowingSupplier</code></li>
  </ul>
  
  <p>This is the related code (<code>BaseEntity</code> is just a base class for entities in my domain, as you might guess):</p>
  
  <pre class="lang-java prettyprint-override"><code>// ThrowingSupplier.java
  @FunctionalInterface
  public interface ThrowingSupplier&lt;T extends BaseEntity&gt; {
      T get() throws RepositoryException;
  }
  
  /* ------------------------------------------------------ */
  
  // ExceptionManager.java
  public final class ExceptionManager&lt;T extends BaseEntity&gt; {
  
      private T result;
      private String exceptionMessage;
  
      ExceptionManager() {
      }
  
      public boolean catcher(ThrowingSupplier&lt;T&gt; supplier) {
          try {
              clearResult();
              clearExceptionMessage();
              result = supplier.get();
              return true;
          } catch (RepositoryException e) {
              exceptionMessage = e.getMessage();
          }
          return false;
      }
  
      // public getters and 'clearers' for attributes
      ...
  
  }
  </code></pre>
  
  <p>And this is how I am using this now:</p>
  
  <pre class="lang-java prettyprint-override"><code>...
  em = new ExceptionManager();
  ... // more code
  if (!em.catcher(() -&gt; repository.findByTitle(course.getTitle()))) {
      view.showError(em.getExceptionMessage(), course);
      return;
  }
  existingCourse = em.getResult();
  ... // some more code
  </code></pre>
  
  <p>Now it seems to me that this does not give any advantages with respect to using directly the try catch in every repository invocation. This is mainly because I need both the return value of the repository method and a way to tell the caller if the repository call has been successful. As a variation I tried to add the <code>showError</code> call inside catcher, but then I must pass <code>view</code> and <code>entity</code> in every invocation of <code>catcher</code>, which I do not like very much as it makes the code less readable.</p>
  
  <p>Is there another way to accomplish this in an elegant manner or it is better to leave the try catch in every call to the repository? Also, what is the standard way to deal with this problem?</p>