组合lambdas和multi-catch子句时的Java bug?

时间:2014-09-13 01:50:57

标签: java exception lambda java-8 multi-catch

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();
    }                         
}

为什么这个程序打印“错误”?如果我删除了lambda,或者如果我拆开了multi-catch子句,那么它会打印“right”。

$ 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)

2 个答案:

答案 0 :(得分:11)

这是1.8.0_20中的fixed bug,从1.8.0_11开始:

  

区域:tools / javac
  概要:javac为lambda中的多捕获语句生成不正确的异常表

     

纠正了lambda中多次捕获的try-catch的处理。

实际的错误报告是JDK-8036942

实际出现的错误是编译器中假定的类型信息丢失:

  

LTM在变量的转换和映射过程中大量使用了擦除()。在大多数情况下,这些擦除操作可能是正确的,但这可能导致信息丢失,如本案例所示。也可以说这里需要如此强烈地使用erasure(),因为LTM是在TransTypes之后应用的,它应该删除大多数/所有类型,所以我想知道这可能是TransTypes中的一个错误。我认为它应该由罗伯特·菲尔德评估,他是LTM的当前维护者,这里最好的方法是什么,我将把它重新分配给他。

我在8u20上看到的内容(我忘了给出一个命令行参数,不再有8u20来正确执行):

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% 

正确:

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,  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

  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
}

答案 1 :(得分:10)

此错误已通过1.8.0_20 https://bugs.openjdk.java.net/browse/JDK-8036942

修复

我能够复制 1.8.0_11 ,并通过 1.8.0_20

修复
$ 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)
$ javac Test.java
$ java Test
wrong

工作正常

~$ javac -version
javac 1.8.0_20
$ javac Test.java 
$ 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)
$ java Test
right