Note that there are some explanatory texts on larger screens.

plurals
  1. POJava 8: Mandatory checked exceptions handling in lambda expressions. Why mandatory, not optional?
    primarykey
    data
    text
    <p>I'm playing with the new lambda features in Java 8, and found that the practices offered by Java 8 are really useful. However, I'm wondering is there a <em>good</em> way to make a work-around for the following scenario. Suppose you have an object pool wrapper that requires some kind of a factory to fill the object pool, for example (using <code>java.lang.functions.Factory</code>):</p> <pre class="lang-java prettyprint-override"><code>public class JdbcConnectionPool extends ObjectPool&lt;Connection&gt; { public ConnectionPool(int maxConnections, String url) { super(new Factory&lt;Connection&gt;() { @Override public Connection make() { try { return DriverManager.getConnection(url); } catch ( SQLException ex ) { throw new RuntimeException(ex); } } }, maxConnections); } } </code></pre> <p>After transforming the functional interface into lambda expression, the code above becomes like that:</p> <pre class="lang-java prettyprint-override"><code>public class JdbcConnectionPool extends ObjectPool&lt;Connection&gt; { public ConnectionPool(int maxConnections, String url) { super(() -&gt; { try { return DriverManager.getConnection(url); } catch ( SQLException ex ) { throw new RuntimeException(ex); } }, maxConnections); } } </code></pre> <p>Not so bad indeed, but the checked exception <code>java.sql.SQLException</code> requires a <code>try</code>/<code>catch</code> block inside the lambda. At my company we use two interfaces for long time:</p> <ul> <li><code>IOut&lt;T&gt;</code> that is an equivalent to <code>java.lang.functions.Factory</code>;</li> <li>and a special interface for the cases that usually require checked exceptions propagation: <code>interface IUnsafeOut&lt;T, E extends Throwable&gt; { T out() throws E; }</code>.</li> </ul> <p>Both <code>IOut&lt;T&gt;</code> and <code>IUnsafeOut&lt;T&gt;</code> are supposed to be removed during migration to Java 8, however there is no exact match for <code>IUnsafeOut&lt;T, E&gt;</code>. If the lambda expressions could deal with checked exceptions like they were unchecked, it could be possible to use simply like the following in the constructor above:</p> <pre class="lang-java prettyprint-override"><code>super(() -&gt; DriverManager.getConnection(url), maxConnections); </code></pre> <p>That looks much cleaner. I see that I can rewrite the <code>ObjectPool</code> super class to accept our <code>IUnsafeOut&lt;T&gt;</code>, but as far as I know, Java 8 is not finished yet, so could be there some changes like:</p> <ul> <li>implementing something similar to <code>IUnsafeOut&lt;T, E&gt;</code>? (to be honest, I consider that dirty - the subject must choose what to accept: either <code>Factory</code> or "unsafe factory" that cannot have compatible method signatures)</li> <li>simply ignoring checked exceptions in lambdas, so no need in <code>IUnsafeOut&lt;T, E&gt;</code> surrogates? (why not? e.g. another important change: OpenJDK, that I use, <code>javac</code> now does not require variables and parameters to be declared as <code>final</code> to be captured in an anonymous class [functional interface] or lambda expression)</li> </ul> <p>So the question is generally is: is there a way to bypass checked exceptions in lambdas or is it planned in the future until Java 8 is finally released?</p> <hr> <p>Update 1</p> <p>Hm-m-m, as far as I understand what we currently have, it seems there is no way at the moment, despite the referenced article is dated from 2010: <a href="https://web.archive.org/web/20170313093438/blogs.oracle.com/briangoetz/entry/exception_transparency_in_java" rel="nofollow noreferrer">Brian Goetz explains exception transparency in Java</a>. If nothing changed much in Java 8, this could be considered an answer. Also Brian says that <code>interface ExceptionalCallable&lt;V, E extends Exception&gt;</code> (what I mentioned as <code>IUnsafeOut&lt;T, E extends Throwable&gt;</code> out of our code legacy) is pretty much useless, and I agree with him.</p> <p>Do I still miss something else?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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