Multiple choice technology programming languages

When is a finally{} block executed?

  1. Only when an unhandled exception is thrown in a try{} block.

  2. Only when any exception is thrown in a try{} block.

  3. Always after execution has left a try{} block, no matter for what reason.

  4. Always just as a method is about to finish.

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

A finally block is ALWAYS executed after execution leaves the try block, regardless of how it exits - whether via normal completion, a caught or uncaught exception, return, break, or continue statements. This guarantee makes finally blocks ideal for cleanup code that must run under all circumstances. The finally block executes even when no exception occurs or when the exception is caught.