Bird
0
0

Find the error in this PHP code snippet:

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

Find the error in this PHP code snippet:

try {
    // some code
} catch (Exception $e) {
    echo "General exception";
} catch (InvalidArgumentException $e) {
    echo "Invalid argument";
}
AMissing semicolon after catch blocks
BTry block is empty, causing syntax error
CInvalidArgumentException cannot be caught separately
DThe catch blocks are in wrong order; specific exceptions must come first
Step-by-Step Solution
Solution:
  1. Step 1: Understand catch block order importance

    More specific exceptions must be caught before more general ones to avoid unreachable code.
  2. Step 2: Identify the order problem

    The general Exception catch block is first, so the InvalidArgumentException catch block is unreachable and causes an error.
  3. Final Answer:

    The catch blocks are in wrong order; specific exceptions must come first -> Option D
  4. Quick Check:

    Specific catch before general catch [OK]
Quick Trick: Place specific catch blocks before general ones [OK]
Common Mistakes:
  • Putting general catch before specific catch
  • Ignoring unreachable catch block warnings
  • Assuming order does not matter

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes