Bird
0
0

Why does the finally block run even if there is a return statement inside the try or catch block?

hard📝 Conceptual Q10 of 15
PHP - Error and Exception Handling

Why does the finally block run even if there is a return statement inside the try or catch block?

ABecause PHP ignores <code>return</code> statements inside <code>try</code> or <code>catch</code> blocks.
BBecause <code>finally</code> is designed to execute cleanup code regardless of how the block exits.
CBecause <code>finally</code> only runs when there is no exception.
DBecause <code>return</code> statements are delayed until after <code>finally</code> finishes.
Step-by-Step Solution
Solution:
  1. Step 1: Understand purpose of finally block

    finally is meant to run cleanup or final steps no matter how the try-catch ends.
  2. Step 2: Explain interaction with return

    Even if return is called, finally runs before the function actually returns.
  3. Final Answer:

    finally runs to execute cleanup regardless of block exit. -> Option B
  4. Quick Check:

    finally always runs for cleanup [OK]
Quick Trick: finally runs for cleanup even with return [OK]
Common Mistakes:
  • Thinking return skips finally
  • Believing finally runs only without exceptions
  • Confusing finally with catch

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes