Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q13 of 15
PHP - Error and Exception Handling

What will be the output of this PHP code?

try {
    echo "Start\n";
    throw new Exception("Error happened");
    echo "End\n";
} catch (Exception $e) {
    echo "Caught: " . $e->getMessage() . "\n";
}
echo "Done\n";
AStart Caught: Error happened Done
BStart Done
CCaught: Error happened Done
DStart End Caught: Error happened Done
Step-by-Step Solution
Solution:
  1. Step 1: Trace code inside try block

    "Start" is printed, then an Exception is thrown, so the next echo "End" is skipped.
  2. Step 2: Catch block runs and prints message

    The catch block prints "Caught: Error happened" and then after try-catch, "Done" is printed.
  3. Final Answer:

    Start Caught: Error happened Done -> Option A
  4. Quick Check:

    Exception stops try, catch prints message, then Done [OK]
Quick Trick: Exception skips rest of try, catch runs, then code continues [OK]
Common Mistakes:
  • Thinking code after throw runs
  • Missing catch block output
  • Assuming program stops after exception

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes