JPad
Snippets
Download
Help
Contact
Interesting try finally trick
The result returned is not the one you expected?
Code
public static void main(String... args) { System.out.println(foo()); } static int foo() { int result = 1; try { return result; } finally { result++; } }
Result
Console
1
Bytecode
Compiled from "RunnContainer.java" public class io.jpad.scratch.RunnContainer { public io.jpad.scratch.RunnContainer(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: return public static void main(java.lang.String...); Code: 0: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream; 3: invokestatic #3 // Method foo:()I 6: invokevirtual #4 // Method java/io/PrintStream.println:(I)V 9: return static int foo(); Code: 0: iconst_1 1: istore_0 2: iload_0 3: istore_1 4: iinc 0, 1 7: iload_1 8: ireturn 9: astore_2 10: iinc 0, 1 13: aload_2 14: athrow Exception table: from to target type 2 4 9 any }
Jpad Version:1.06
viewCount:3524
Creator:
admin
Latest Editor:
admin
Tags
try
finally
Download
Copy As Markdown:
**code [Interesting try finally trick.java](/example/1D/interesting-try-finally-trick)** public static void main(String... args) { System.out.println(foo()); } static int foo() { int result = 1; try { return result; } finally { result++; } } **Result:** 1