Bird
0
0

Consider this PHP code snippet:

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

Consider this PHP code snippet:

try {
  throw new Error('Fatal error');
} catch (Exception $e) {
  echo 'Exception caught';
} catch (Throwable $e) {
  echo 'Throwable caught';
}

What will be the output?

AException caught
BThrowable caught
CFatal error message printed
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Identify thrown error type

    The code throws an Error, which is not an Exception but implements Throwable.
  2. Step 2: Match catch blocks

    The first catch block is for Exception (no match). The second catch block is for Throwable (matches Error).
  3. Final Answer:

    Throwable caught -> Option B
  4. Quick Check:

    Error caught by Throwable catch block [OK]
Quick Trick: Throwable catch block handles both Exception and Error [OK]
Common Mistakes:
  • Assuming Error is caught by Exception catch
  • Expecting fatal error to print automatically
  • Ignoring Throwable interface

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes