Java 8 generics + exceptions compile time error when using a lambda expression -


A few days ago, I started refactoring some code to use the new Java 8 stream library. Unfortunately, I was running in a compile time error when the stream :: map is shown with a method that is declared to throw the normal e which is further specified as a runtime exception.

Sufficient interesting, compile time error away when I switch to using a method reference.

Is this a bug, or is not it similar to my Lambda expression?

(Also, I know that with the ----- p.execute (foo) parameter :: execute, there are additional parameters for the execute method in my actual code.)


Error message

  Error: (32, 43) java: unreported exception E;   code  

  import java.util.ArrayList; Import java.util.list; Public Class JavaBug Test {Interface AbleToThrowException & lt; E expense & gt; {} Interface parameter {public & lt; Exception expands in E & gt; Object execution (AbleToThrowException & lt; E & gt; algo) e throws; } Interface ThrowsRuntimeException AbleToThrowException Extended & lt; RuntimeException & gt; {} Static throgantum exposition foo; Public static object manual lambda (parameter p) {return p. Execute (FU); } Public static zero main (string [] args) {List & lt; Parameter & gt; Parameters = new arreelist & lt; & Gt; (); Params.stream (). Map (P -> P. Execute (Foo)); // returns a compile time error. Params.stream () Map (JavaBugTest :: manualLambda); // works fine. OS: Windows X64  
  • Java compiler version:
  • IDE: Intellij

    A very simple solution is to clearly provide a type of argument for parameter # execution (..) .

      params.stream (). Map (p - & gt; p. & Lt; RuntimeException & gt; execute (foo)); // A compile time gives error without obviously logic, it seems that the JDK compiler can not predict a type of logic with reference to orientation, although it should be. It is a bug and should be reported in such a way that I have now notified it and I will update this information with new information. 


  • Comments

    Popular posts from this blog

    java - org.apache.http.ProtocolException: Target host is not specified -

    java - Gradle dependencies: compile project by relative path -

    ruby on rails - Object doesn't support #inspect when used with .include -