Exceptions and lambda.

Srikanth S Adayapalam srikanth_sankaran at in.ibm.com
Tue Feb 5 20:36:16 PST 2013


The following lambda expression does not compile with 8b74, while the
implementation of the method in a class does compile: 

// ------------
interface G1 {
  <E extends Exception> Object m(E p) throws E;
}
interface G2 {
  <F extends Exception> String m(F q) throws Exception;
}
interface G extends G1, G2 {}

// G has descriptor <F extends Exception> ()->String throws F

public class X {
 
        G g = (x) -> { // Elided type is inferred from descriptor to be F
            throw x;    // ~== throw new F()
        };
}

class Y implements G {
        public <T extends Exception> String m(T t) throws T {
                throw t;
        }
}
// -----------------------

Is this a bug in the implementation or the specification ?
Since the lambda cannot refer to the type variables of the
descriptor by name nor declare its own, if 8b74 behavior is 
correct, that would mean that, no checked exceptions could
be thrown from a lambda if the descriptor of the target
interface method mentions a type variable in throws clause.

My analysis is that the lambda should be accepted as it does
not manufacture any fresh exceptions and simply throws an
exception that is known to have satisfied the constraints at
the invocation site, i.e no additional/unexpected surprises
are there due the lambda throwing the object it was handed.

Thanks in advance for your clarification.
Srikanth. 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/lambda-spec-experts/attachments/20130205/39b47aab/attachment.html 


More information about the lambda-spec-experts mailing list