[
https://issues.apache.org/jira/browse/GROOVY-8085?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15877791#comment-15877791
]
Daniel Sun commented on GROOVY-8085:
------------------------------------
{code}
// test code
try {
try {
System.out.println("try!!!");
} finally {
System.out.println(99 / 0);
}
} catch (Exception e) {
System.out.println("catch!!!");
}
{code}
https://github.com/apache/groovy/blob/master/src/main/org/codehaus/groovy/classgen/asm/StatementWriter.java#L584
Generating bytecodes for ReturnStatement(i.e. return
System.out.println("try!!!");) will record ranges of blocks, the range of the
outer block is not recorded correctly(not cover the finally block in the normal
execution flow).
https://github.com/apache/groovy/blob/master/src/main/org/codehaus/groovy/classgen/asm/CompileStack.java#L770
{code}
private void applyBlockRecorder(List<BlockRecorder> blocks) {
if (blocks.isEmpty() || blocks.size()==visitedBlocks.size()) return;
MethodVisitor mv = controller.getMethodVisitor();
Label end = new Label();
mv.visitInsn(NOP);
mv.visitLabel(end);
Label newStart = new Label();
for (BlockRecorder fb : blocks) {
if (visitedBlocks.contains(fb)) continue;
fb.closeRange(end);
// we exclude the finally block from the exception table
// here to avoid double visiting of finally statements
fb.excludedStatement.run();
fb.startRange(newStart);
}
mv.visitInsn(NOP);
mv.visitLabel(newStart);
}
{code}
The following bytecodes have not been covered by any exception table!
{code}
L9
LINENUMBER 6 L9
ALOAD 1
LDC 3
AALOAD
ALOAD 1
LDC 4
AALOAD
LDC Ljava/lang/System;.class
INVOKEINTERFACE
org/codehaus/groovy/runtime/callsite/CallSite.callGetProperty
(Ljava/lang/Object;)Ljava/lang/Object;
ALOAD 1
LDC 5
AALOAD
BIPUSH 99
INVOKESTATIC java/lang/Integer.valueOf (I)Ljava/lang/Integer;
ICONST_0
INVOKESTATIC java/lang/Integer.valueOf (I)Ljava/lang/Integer;
INVOKEINTERFACE org/codehaus/groovy/runtime/callsite/CallSite.call
(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
INVOKEINTERFACE org/codehaus/groovy/runtime/callsite/CallSite.call
(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
POP
{code}
*logs(https://github.com/apache/groovy/pull/503):*
{code}
L3
LINENUMBER 2 L3
L4
L5
LINENUMBER 3 L5
L6
L7
LINENUMBER 4 L7
ALOAD 1
LDC 1
AALOAD
ALOAD 1
LDC 2
AALOAD
LDC Ljava/lang/System;.class
INVOKEINTERFACE
org/codehaus/groovy/runtime/callsite/CallSite.callGetProperty
(Ljava/lang/Object;)Ljava/lang/Object;
LDC "try!!!"
INVOKEINTERFACE org/codehaus/groovy/runtime/callsite/CallSite.call
(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
ASTORE 2
NOP
L8
L9
LINENUMBER 6 L9
ALOAD 1
LDC 3
AALOAD
ALOAD 1
LDC 4
AALOAD
LDC Ljava/lang/System;.class
INVOKEINTERFACE
org/codehaus/groovy/runtime/callsite/CallSite.callGetProperty
(Ljava/lang/Object;)Ljava/lang/Object;
ALOAD 1
LDC 5
AALOAD
BIPUSH 99
INVOKESTATIC java/lang/Integer.valueOf (I)Ljava/lang/Integer;
ICONST_0
INVOKESTATIC java/lang/Integer.valueOf (I)Ljava/lang/Integer;
INVOKEINTERFACE org/codehaus/groovy/runtime/callsite/CallSite.call
(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
INVOKEINTERFACE org/codehaus/groovy/runtime/callsite/CallSite.call
(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
POP
L10
L11
NOP
L12
ALOAD 2
ARETURN
L13
GOTO L14
L15
L16
TRYCATCHBLOCK L6 L8 L17 null
TRYCATCHBLOCK L12 L15 L17 null
L18
L14
ALOAD 1
LDC 6
AALOAD
ALOAD 1
LDC 7
AALOAD
LDC Ljava/lang/System;.class
INVOKEINTERFACE
org/codehaus/groovy/runtime/callsite/CallSite.callGetProperty
(Ljava/lang/Object;)Ljava/lang/Object;
ALOAD 1
LDC 8
AALOAD
BIPUSH 99
INVOKESTATIC java/lang/Integer.valueOf (I)Ljava/lang/Integer;
ICONST_0
INVOKESTATIC java/lang/Integer.valueOf (I)Ljava/lang/Integer;
INVOKEINTERFACE org/codehaus/groovy/runtime/callsite/CallSite.call
(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
INVOKEINTERFACE org/codehaus/groovy/runtime/callsite/CallSite.call
(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
POP
L19
L20
NOP
GOTO L21
L17
ASTORE 3
ALOAD 1
LDC 9
AALOAD
ALOAD 1
LDC 10
AALOAD
LDC Ljava/lang/System;.class
INVOKEINTERFACE
org/codehaus/groovy/runtime/callsite/CallSite.callGetProperty
(Ljava/lang/Object;)Ljava/lang/Object;
ALOAD 1
LDC 11
AALOAD
BIPUSH 99
INVOKESTATIC java/lang/Integer.valueOf (I)Ljava/lang/Integer;
ICONST_0
INVOKESTATIC java/lang/Integer.valueOf (I)Ljava/lang/Integer;
INVOKEINTERFACE org/codehaus/groovy/runtime/callsite/CallSite.call
(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
INVOKEINTERFACE org/codehaus/groovy/runtime/callsite/CallSite.call
(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
POP
L22
L23
ALOAD 3
ATHROW
L21
L24
GOTO L25
L26
L27
L28
ASTORE 4
L29
L30
LINENUMBER 9 L30
ALOAD 1
LDC 12
AALOAD
ALOAD 1
LDC 13
AALOAD
LDC Ljava/lang/System;.class
INVOKEINTERFACE
org/codehaus/groovy/runtime/callsite/CallSite.callGetProperty
(Ljava/lang/Object;)Ljava/lang/Object;
LDC "catch!!!"
INVOKEINTERFACE org/codehaus/groovy/runtime/callsite/CallSite.call
(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
ASTORE 5
NOP
L31
NOP
L32
ALOAD 5
ARETURN
L33
NOP
L34
L35
GOTO L25
TRYCATCHBLOCK L4 L8 L28 java/lang/Exception
TRYCATCHBLOCK L12 L26 L28 java/lang/Exception
TRYCATCHBLOCK L4 L8 L36 null
TRYCATCHBLOCK L12 L26 L36 null
TRYCATCHBLOCK L28 L31 L36 null
TRYCATCHBLOCK L32 L35 L36 null
L37
L25
NOP
GOTO L38
L36
ASTORE 6
ALOAD 6
ATHROW
L38
{code}
> Exception in "finally" not caught by outer "try"
> ------------------------------------------------
>
> Key: GROOVY-8085
> URL: https://issues.apache.org/jira/browse/GROOVY-8085
> Project: Groovy
> Issue Type: Bug
> Affects Versions: 2.4.8
> Environment: linux
> Reporter: Craig Silverstein
>
> I would expect the following code to print `caughtt`:
> ```
> groovy -e 'try { try { true; } finally { 1 / 0 } } catch (e) { println
> "caughtt" }'
> ```
> But instead, it prints:
> ```
> Caught: java.lang.ArithmeticException: Division by zero
> java.lang.ArithmeticException: Division by zero
> at script_from_command_line.run(script_from_command_line:1)
> ```
> Why is the exception, thrown by the `finally`, not being caught by the outer
> try/catch?
--
This message was sent by Atlassian JIRA
(v6.3.15#6346)