Lambda and multi-catch

Vicente-Arturo Romero-Zaldivar vicente.romero at oracle.com
Mon Mar 10 16:04:13 UTC 2014


Hi Ivan,

Thanks for your patch. After analyzing the code in LambdaToMethod, I 
think that this is not the best solution for the problem. We should 
analyze if there is a general solution here. This bug may indicate that 
some more statements may be affected. I will add my findings in the bug 
entry [1] along with your proposed patch as a reference.

Thanks,
Vicente

[1] https://bugs.openjdk.java.net/browse/JDK-8036942

On 08/03/14 18:31, Ivan Babanin wrote:
>> A colleague of mine have find a hairy bug,
>> here is a simple code to reproduce it.
>> import java.io.IOException;
>> public class FunWithMultiCatch {
>>     public static void main(String[] args) {
>>       Runnable r = () -> {
>>         try {
>>           Object o = null;
>>           o.getClass();
>>          throw new IOException();
>>        } catch(IOException | IllegalArgumentException e) {
>>          System.out.println("KO !");
>>        } catch(RuntimeException e) {
>>          System.out.println("OK !");
>>        }
>>      };
>>      r.run();
>>     }
>> }
>> It prints 'KO !' :(
>> The problem can be reproduced for any multi-catch inside a lambda,
>> the exception table is clearly wrong, the IOException and IAE are merged
>> to their common supertype (java.lang.Exception).
> I got source from http://hg.openjdk.java.net/jdk8u/jdk8u
>
> After a brief analysis, I found the problem in
> LambdaToMethod.visitVarDef(...):438.
> Tree translator creates JCVariableDecl (line 444) and ignores vartype of
> original tree,
> that's why Gen. genCatch(...) generate only single catch in exception table.
>
> I think my code (surrounded by comments) will fix this bug:
>
> result =
> make.at(tree).VarDef((VarSymbol)lambdaContext.getSymbolMap(LOCAL_VAR).get(tr
> ee.sym), init);
> // Copy vartype from original tree for variables with multiple types
> if (tree.vartype.hasTag(TYPEUNION)) {
>     ((JCVariableDecl) result).vartype = tree.vartype;
> }
> //
>
> sy,
> Ivan
>
>



More information about the lambda-dev mailing list