Bird
0
0

Identify the error in this PHP code snippet:

medium📝 Debug Q6 of 15
PHP - Error and Exception Handling
Identify the error in this PHP code snippet:
try {
  throw new Exception('Error!');
} catch (Error $e) {
  echo 'Caught Error';
}
AThe catch block must come before try block.
BThe throw statement is missing a semicolon.
CThe try block cannot throw exceptions.
DThe catch block should catch Exception, not Error.
Step-by-Step Solution
Solution:
  1. Step 1: Check thrown object type

    The code throws an Exception object.
  2. Step 2: Check catch block type

    The catch block is for Error, which does not catch Exception.
  3. Final Answer:

    The catch block should catch Exception, not Error. -> Option D
  4. Quick Check:

    Catch type must match thrown exception [OK]
Quick Trick: Catch block type must match thrown exception type [OK]
Common Mistakes:
  • Catching wrong exception type
  • Misplacing catch block
  • Syntax errors in throw statement

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes