Why will the code finally execute even if it returns to the try block?

the code:

public String get() {
try {
     //doSomething
     return "Hello";
}
finally {
     System.out.print("Finally");
}

code> How is this code executed?

+3
source share
3 answers

Because the whole point of the block finallyis it executes, however you leave the block try, unless the virtual machine itself is disconnected.

finally - , try, ? return, , . finally , , .

. JLS 14.20.2 - , finally.

+17

, , , try (fail, return, exception, finish ..).

, , try/catch/finally.

+3

This is what it means finally: the code inside will be executed when the block remains try, no matter how (with the exception of disabling the JVM through System.exit()or external reasons).

0
source

Source: https://habr.com/ru/post/1791212/


All Articles