Bird
0
0

Find the error in this PHP code:

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

Find the error in this PHP code:

class MyException extends Exception {
  public function __construct($message) {
    parent::__construct($message);
  }
}

try {
  throw new MyException();
} catch (MyException $e) {
  echo $e->getMessage();
}
ANo error, outputs empty string
BFatal error: Missing argument for MyException constructor
CSyntax error: wrong constructor declaration
DRuntime error: uncaught exception
Step-by-Step Solution
Solution:
  1. Step 1: Check constructor parameters

    The constructor requires one parameter $message but none is passed when throwing.
  2. Step 2: Analyze error

    PHP throws a fatal error for missing required argument in constructor.
  3. Final Answer:

    Fatal error: Missing argument for MyException constructor -> Option B
  4. Quick Check:

    Constructor requires argument but none given [OK]
Quick Trick: Pass required arguments when throwing exceptions with custom constructors [OK]
Common Mistakes:
  • Assuming default constructor is called
  • Ignoring required parameters in constructor
  • Expecting empty message output without argument

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes