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?
try {
  throw new Error('Fatal error');
} catch (Exception $e) {
  echo 'Caught Exception';
} catch (Error $e) {
  echo 'Caught Error';
}
ACaught Error
BCaught Exception
CFatal error
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Analyze thrown object type

    The code throws an Error object, not an Exception.
  2. Step 2: Check catch blocks order and matching

    The first catch is for Exception, which does not match Error. The second catch matches Error, so it executes.
  3. Final Answer:

    Caught Error -> Option A
  4. Quick Check:

    Error caught by matching catch block = Caught Error [OK]
Quick Trick: Catch blocks must match thrown object type [OK]
Common Mistakes:
  • Assuming Exception catch block catches Error
  • Ignoring catch block order
  • Expecting no output due to error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes