Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Just after writing the question started thinking about utility method powered with generics. Then remembered something about <a href="http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/base/Throwables.html" rel="nofollow noreferrer">Throwables</a>. And yes, it's already there! )</p> <p>It may also be necessary to handle <a href="https://github.com/google/guava/issues/2867" rel="nofollow noreferrer">UncheckedExecutionException or even ExecutionError</a>.</p> <p>So the solution is:</p> <pre><code>public Post getPost(final Integer key) throws SQLException, IOException { try { return cache.get(key, new Callable&lt;Post&gt;() { @Override public Post call() throws Exception { return PostsDB.findPostByID(key); } }); } catch (ExecutionException e) { Throwables.propagateIfPossible( e.getCause(), SQLException.class, IOException.class); throw new IllegalStateException(e); } catch (UncheckedExecutionException e) { Throwables.throwIfUnchecked(e.getCause()); throw new IllegalStateException(e); } } </code></pre> <p>Very nice!</p> <p>See also <a href="https://code.google.com/p/guava-libraries/wiki/ThrowablesExplained#Controversial:_Exception_tunneling" rel="nofollow noreferrer">ThrowablesExplained</a>.</p> <p>See also <a href="https://github.com/google/guava/wiki/Why-we-deprecated-Throwables.propagate" rel="nofollow noreferrer">Why we deprecated Throwables.propagate</a> and <a href="https://google.github.io/guava/releases/snapshot/api/docs/com/google/common/cache/LoadingCache.html#getUnchecked-K-" rel="nofollow noreferrer">LoadingCache.getUnchecked</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload