Bird
0
0

Identify the error in this PHP code snippet:

medium📝 Debug Q14 of 15
PHP - Error and Exception Handling
Identify the error in this PHP code snippet:
try {
    throw new Exception("Error!");
} catch (Exception $e) {
    echo $e->message();
}
AException class is not imported.
BMissing semicolon after <code>throw</code> statement.
CMethod <code>message()</code> does not exist; should use <code>getMessage()</code>.
DCatch block syntax is incorrect.
Step-by-Step Solution
Solution:
  1. Step 1: Check the catch block method call

    The Exception class has a method getMessage() to get the error message, not message().
  2. Step 2: Verify other syntax elements

    The semicolon after throw is present, Exception class is built-in, and catch syntax is correct.
  3. Final Answer:

    Method message() does not exist; should use getMessage(). -> Option C
  4. Quick Check:

    Use getMessage() to get exception text [OK]
Quick Trick: Use getMessage() method to read exception message [OK]
Common Mistakes:
  • Calling non-existent message() method
  • Missing semicolons (not in this case)
  • Incorrect catch syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes