Bird
0
0

What happens if a return statement is executed inside a try block but there is also a finally block?

easy📝 Conceptual Q2 of 15
PHP - Error and Exception Handling
What happens if a return statement is executed inside a try block but there is also a finally block?
AThe function returns immediately, skipping finally
BThe finally block executes before the function returns
CThe finally block executes only if an exception occurs
DThe finally block is ignored if return is called
Step-by-Step Solution
Solution:
  1. Step 1: Understand return inside try with finally

    When return is called in try, PHP still executes the finally block before returning.
  2. Step 2: Confirm finally always runs

    Finally runs even if return is called, ensuring cleanup or final actions happen.
  3. Final Answer:

    The finally block executes before the function returns -> Option B
  4. Quick Check:

    finally runs before return completes [OK]
Quick Trick: finally runs even if try returns early [OK]
Common Mistakes:
  • Assuming return skips finally
  • Thinking finally runs only on exceptions
  • Believing finally runs after return completes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes