Inferring that what exceptions are thrown from a lambda

Esko Luontola esko.luontola at gmail.com
Fri Sep 6 10:19:41 PDT 2013


Stuart Marks wrote on 4.9.2013 23:11:
> A change to support this went in fairly recently. This now compiles for
> me using JDK 8 b105. It fails with the error you mention when using
> older builds, e.g., JDK 8 b88, which is one I happened to have lying
> around. (Note, I am referring to JDK 8 builds, not Lambda builds.)

I think I found a bug related to that in 1.8.0-ea-b105: when I compile 
both files with the same javac command, it compiles, but if I compile 
them separately, it doesn't compile.

Here is how to reproduce the issue. I used the 
jdk-8-ea-bin-b105-windows-x64-29_aug_2013.exe build.

-- Foo.java:
import java.io.IOException;

class Foo {

     public void compiles() throws IOException {
         String result = Bar.tryRepeatedly(10, () -> {
             throw new IOException("dummy exception");
         });
     }

     public void doesNotCompile() {
         String result = Bar.tryRepeatedly(10, () -> "result");
     }

     public void compilesButUndesirable() throws Throwable {
         String result = Bar.tryRepeatedly(10, () -> "result");
     }
}
--

-- Bar.java:
class Bar {
     public static <T, E extends Throwable> T tryRepeatedly(int 
maxTries, Action<T, E> action) throws E {
         return action.run();
     }
}

interface Action<T, E extends Throwable> {
     T run() throws E;
}
--

This command succeeds:

$ javac Foo.java Bar.java

But this series of commands fails:

$ javac Bar.java

$ javac Foo.java
Foo.java:12: error: unreported exception Throwable; must be caught or 
declared to be thrown
         String result = Bar.tryRepeatedly(10, () -> "result");
                                          ^
1 error


-- 
Esko Luontola
www.orfjackal.net


More information about the lambda-dev mailing list