Java return vs finally

Even though return statement present in try or catch blocks first finally will be executed and after that only return statement will be considered. i.e. finally block dominates the return statement

Example – return vs finally

Output :

main begin
try block : Risky code
finally block : Always executes

In the above example, even there is a ‘return’ statement in the try block we could see that finally block got executed.

Generally when we write a ‘return’ in any method, then after return executed then no other statement executes. But in the case of finally, finally block executed first and then return.

If return statement present inside try, catch, finally then finally block return statement will be considered.

Scroll to Top