Bird
0
0

Given this PHP code, what will be the output?

hard📝 Application Q8 of 15
PHP - Error and Exception Handling

Given this PHP code, what will be the output?

try {
  throw new Exception('General');
} catch (Error $e) {
  echo 'Error caught';
} catch (Exception $e) {
  echo 'Exception caught';
} catch (Throwable $e) {
  echo 'Throwable caught';
}
AException caught
BError caught
CThrowable caught
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Identify thrown exception

    The code throws an Exception.
  2. Step 2: Match catch blocks in order

    First catch is for Error (no match), second is Exception (match), so it executes.
  3. Final Answer:

    Exception caught -> Option A
  4. Quick Check:

    Exception matches second catch block [OK]
Quick Trick: Catch blocks run first matching type in order [OK]
Common Mistakes:
  • Assuming Throwable catch runs first
  • Expecting Error catch to handle Exception
  • Ignoring catch block order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes