Inferring that what exceptions are thrown from a lambda
Howard Lovatt
howard.lovatt at gmail.com
Wed Sep 4 19:22:34 PDT 2013
It is generally easier to do this:
public interface Action<T> {
T run() throws Exception;
}
Which is what Callable does, infact the above is Callable apart from the
name changes (Callable -> Action, call -> run).
On 5 September 2013 06:11, Stuart Marks <stuart.marks at oracle.com> wrote:
> On 8/31/13 11:04 AM, Esko Luontola wrote:
> > But if the lambda doesn't thrown anything, the compiler thinks that the
> > method may throw the most generic exception. The following code fails to
> > compile with "error: unreported exception Throwable; must be caught or
> > declared to be thrown"
> >
> > public void doesNotCompile() {
> > String result = Resilient.tryRepeatedly(10, () -> "result");
> > }
>
> 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 believe that if a lambda throws no checked exceptions, and its
> functional interface method is declared "throws E", then E is now
> inferred to be RuntimeException.
>
> s'marks
>
>
--
-- Howard.
More information about the lambda-dev
mailing list