Java error when combining Lambdas and multiple catch clauses?
•
Java
import java.io.*;
import java.io.*; import java.net.*; public class Test { public static void main(String[] arguments) throws Exception { Runnable runnable = () -> { try { throwException(); } catch (SocketException|EOFException exception) { System.err.println("wrong"); } catch (IOException exception) { System.err.println("right"); } }; runnable.run(); } private static void throwException() throws IOException { throw new NotSerializableException(); } }
Why does this program print "wrong"? If I delete a lambda, or if I separate multiple catch clauses, it prints "yes"
$javac -version javac 1.8.0_11 $java -version java version "1.8.0_11" Java(TM) SE Runtime Environment (build 1.8.0_11-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.11-b03,mixed mode)
Solution
This is 1.8 0_ 20 Fixed bug, from 1.8 0_ 11:
The actual error report is jdk-8036942
The actual error is the loss of type information in the compiler:
I saw it on 8u20 (I forgot to give a command line parameter, and no 8u20 will execute correctly):
wlan1-loopback% /usr/lib/jvm/java-8-oracle/bin/javap -c Test Compiled from "Test.java" public class Test { public test(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: return public static void main(java.lang.String[]) throws java.lang.Exception; Code: 0: invokedynamic #2,0 // InvokeDynamic #0:run:()Ljava/lang/Runnable; 5: astore_1 6: aload_1 7: invokeinterface #3,1 // InterfaceMethod java/lang/Runnable.run:()V 12: return } wlan1-loopback% java Test right wlan1-loopback% java -version java version "1.8.0_20" Java(TM) SE Runtime Environment (build 1.8.0_20-b26) Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23,mixed mode) wlan1-loopback%
correct:
public class Test { public test(); descriptor: ()V Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: return public static void main(java.lang.String[]) throws java.lang.Exception; descriptor: ([Ljava/lang/String;)V Code: 0: invokedynamic #2,1 // InterfaceMethod java/lang/Runnable.run:()V 12: return private static void throwException() throws java.io.IOException; descriptor: ()V Code: 0: new #4 // class java/io/NotSerializableException 3: dup 4: invokespecial #5 // Method java/io/NotSerializableException."<init>":()V 7: athrow private static void lambda$main$0(); descriptor: ()V Code: 0: invokestatic #6 // Method throwException:()V 3: goto 27 6: astore_0 7: getstatic #9 // Field java/lang/System.err:Ljava/io/PrintStream; 10: ldc #10 // String wrong 12: invokevirtual #11 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 15: goto 27 18: astore_0 19: getstatic #9 // Field java/lang/System.err:Ljava/io/PrintStream; 22: ldc #13 // String right 24: invokevirtual #11 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 27: return Exception table: from to target type 0 3 6 Class java/net/SocketException 0 3 6 Class java/io/EOFException 0 3 18 Class java/io/IOException }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码