Here is my simple test code:
class Scratch { public static void main(String[] args) { try { System.out.println("Line 1"); throw new RuntimeException(); } catch (RuntimeException e) { e.printStackTrace(); } finally { System.out.println("Line 2"); } } } After running I'll get this:
Line 1 Line 2 java.lang.RuntimeException at Scratch.main(scratch_4.java:5) Process finished with exit code 0 I thought that "finally" code must be executed at last, but it's not. What is the reason?
catch. I'd have to look that up in the language spec though to be sure.finallyshould make it clear that it's the last thing to happen; but then I spotted that the Oracle tutorial says "The finally block always executes when the try block exits.", which is a pretty misleading statement. Like JonSkeet says: it's definitely executed after the catch, if a matching exception was thrown by the try.