PHP - Error and Exception Handling
How can you add a custom property $errorCode to a custom exception class and access it after catching?
class MyException extends Exception {
private int $errorCode;
public function __construct(string $message, int $code) {
parent::__construct($message);
$this->errorCode = $code;
}
public function getErrorCode(): int {
return $this->errorCode;
}
}
try {
throw new MyException("Failed", 404);
} catch (MyException $e) {
// What to echo here?
}