Bird
0
0

Identify the problem in this PHP code:

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

Identify the problem in this PHP code:

try {
    echo "Start";
    throw new Exception("Oops");
} catch (Error $e) {
    echo "Caught error";
}
AMissing finally block causes error
BException is not caught because catch expects Error type
CSyntax error in throw statement
DCode runs without any output
Step-by-Step Solution
Solution:
  1. Step 1: Check exception type thrown and caught

    The code throws an Exception but tries to catch an Error type.
  2. Step 2: Understand type mismatch effect

    Since Exception and Error are different, the catch block does not handle the thrown Exception.
  3. Final Answer:

    Exception is not caught because catch expects Error type. -> Option B
  4. Quick Check:

    Catch type must match thrown exception [OK]
Quick Trick: Catch block type must match thrown exception type [OK]
Common Mistakes:
  • Assuming catch catches all exceptions
  • Confusing Exception and Error types
  • Thinking finally is required

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes