Bird
0
0

Find the error in this PHP code snippet:

medium📝 Debug Q6 of 15
PHP - Error and Exception Handling

Find the error in this PHP code snippet:

try {
    echo "Test";
} catch Exception $e {
    echo "Error";
}
AThrow statement missing inside try block
BMissing semicolon after echo statement
CCannot catch Exception without a finally block
DMissing parentheses around Exception and variable in catch
Step-by-Step Solution
Solution:
  1. Step 1: Check catch syntax

    The catch block must have parentheses around the exception type and variable.
  2. Step 2: Identify missing parentheses

    Code uses catch Exception $e without parentheses, which is invalid syntax.
  3. Final Answer:

    Missing parentheses around Exception and variable in catch. -> Option D
  4. Quick Check:

    Correct catch syntax requires parentheses [OK]
Quick Trick: Always use parentheses in catch block [OK]
Common Mistakes:
  • Omitting parentheses in catch
  • Thinking semicolon is missing
  • Believing finally is mandatory

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes