Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q4 of 15
PHP - Error and Exception Handling
What will be the output of this PHP code?
function example() {
  try {
    echo "Start ";
    throw new Exception("Error");
  } catch (Exception $e) {
    echo "Catch ";
  } finally {
    echo "Finally";
  }
  echo " End";
}
example();
AStart Catch End Finally
BStart Finally Catch End
CStart Catch Finally End
DStart Finally End Catch
Step-by-Step Solution
Solution:
  1. Step 1: Trace the try block

    "Start " is printed, then exception is thrown.
  2. Step 2: Catch block runs

    Exception caught, prints "Catch ".
  3. Step 3: Finally block runs

    Prints "Finally" regardless of exception.
  4. Step 4: Code after try-catch-finally runs

    Prints " End".
  5. Final Answer:

    Start Catch Finally End -> Option C
  6. Quick Check:

    finally runs after catch before code continues [OK]
Quick Trick: finally runs after catch before next code [OK]
Common Mistakes:
  • Assuming finally runs before catch
  • Forgetting code after finally runs
  • Mixing order of printed words

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes