Avatar ·

Does the finally block always execute in Java? Understanding the nuances

Hello everyone! I'm a beginner Java developer, and I'm confused about the finally block. I've read that finally always executes, regardless of whether an exception is thrown or not. But in practice, I've encountered several situations where this doesn't seem to be the case. Here's an example code I wrote for testing: ```java public class FinallyTest { public static void main(String[] args) { System.out.println("Result: " + testFinally()); } public static int testFinally() { try { System.out.println("In try block"); return 1; } catch (Exception e) { System.out.println("In catch block"); return 2; } finally { System.out.println("In finally block"); return 3; // Here's the