Skip to content

Instantly share code, notes, and snippets.

@wcang
Created February 20, 2022 13:05
Show Gist options
  • Select an option

  • Save wcang/9f083268f45c65144dcac8aa146357b7 to your computer and use it in GitHub Desktop.

Select an option

Save wcang/9f083268f45c65144dcac8aa146357b7 to your computer and use it in GitHub Desktop.
I just realized that the code in finally block will be called when if the method exits in prior code block
public class Finally {
public static int tryFinally() {
try {
return 1;
} finally {
System.out.println("Finally block");
}
}
public static void main(String[] args) {
System.out.println("Answer is " + tryFinally());
}
}
@wcang
Copy link
Author

wcang commented Feb 20, 2022

The output of this code is:
Finally block
Answer is 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment