Bird
0
0

What will be the output of the following PHP code?

medium📝 Predict Output Q13 of 15
PHP - Error and Exception Handling
What will be the output of the following PHP code?
function test() {
  try {
    echo "Try block\n";
    return 1;
  } catch (Exception $e) {
    echo "Catch block\n";
  } finally {
    echo "Finally block\n";
  }
}
echo test();
ATry block 1 Finally block
BTry block Finally block 1
CFinally block Try block 1
DTry block Catch block Finally block 1
Step-by-Step Solution
Solution:
  1. Step 1: Trace the try block execution

    The try block prints "Try block" and then returns 1 immediately.
  2. Step 2: Understand finally block runs after return

    Even though return is called, the finally block executes next, printing "Finally block" before the function returns.
  3. Final Answer:

    Try block Finally block 1 -> Option B
  4. Quick Check:

    finally runs after return = A [OK]
Quick Trick: finally runs even after return statement [OK]
Common Mistakes:
  • Assuming finally does not run after return
  • Expecting catch block to run without exception
  • Misordering output lines

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes