Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q5 of 15
PHP - Error and Exception Handling
What will be the output of this PHP code?
try {
    throw new Exception("Failed");
    echo "This will not print";
} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
}
AError: This will not print
BThis will not print
CNo output
DError: Failed
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the try block

    The throw statement raises an exception with message "Failed" and stops execution inside the try block immediately.
  2. Step 2: Check code after throw

    The echo statement after throw is never executed.
  3. Step 3: Catch block execution

    The catch block catches the exception and prints "Error: Failed".
  4. Final Answer:

    Error: Failed -> Option D
  5. Quick Check:

    Code after throw is skipped [OK]
Quick Trick: Code after throw inside try is skipped [OK]
Common Mistakes:
  • Expecting code after throw to run
  • Ignoring catch block output
  • Assuming no output is produced

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes