Interview Questions

Can a finally block gets invoked if a return statement got executed in try block?

Yes. If a return statement got encountered in try block, before the return happens to the calling method, the finally block gets executed. By this way, whether there is an exception thrown or not in try block or whether any return statement encountered in try block, irrespective of all scenarios, the finally block gets executed.

The only exception scenario is the usage of System.exit() in try block. In this case, the entire JVM will be shut down hence finally won't gets executed