Skip to content

Latest commit

 

History

History
45 lines (37 loc) · 1.28 KB

0698.md

File metadata and controls

45 lines (37 loc) · 1.28 KB

Considering the code below

class AppException extends Exception {
  function __toString() {
    return "Your code has just thrown an exception: {$this->message}\n";
  }
}

class Students {
  public $first_name;
  public $last_name;

  public function __construct($first_name, $last_name) {
    if(empty($first_name)) {
      throw new AppException('First Name is required', 1);
    }

    if(empty($last_name)) {
      throw new AppException('Last Name is required', 2);
    }
  }
}

try {
    new Students('', ''); 
} catch (Exception $e) {
    echo $e;
}

Which of these statements are correct ?

  • A) The magic __toString() method will be invoked automatically when the code enters the catch() statement and the custom exception message will be printed
  • B) The catch (Exception $e) statement is wrong because it accepts an instance of the Exception class as the parameter. It should use an instance of the AppException class instead.
  • C) The __toString() method can't be overwritten in a child class because the methods in the Exception class are all final but for the constructor
  • D) all
Answer

Answer: A