完全遇到未捕获的错误,我在构造函数中使用异常

时间:2019-10-07 18:29:34

标签: php exception constructor typeerror strict

我还添加了以下内容

declare(strict_types = 1);

简短代码测试严格类型

Class UsingStrict {

    private $name;

    function __construct(string $name) {

        try {
            $this->name = $name;
        } catch (TypeError $e) {
            echo "Caught the exception";
        }
    }
}

$variable = new UsingStrict(0);

哥登

Fatal error: Uncaught TypeError: Argument 1 passed to UsingStrict::__construct() must be of the type string, int given

期望

Caught the exception

我对此很陌生,难道无法在构造函数中捕获异常吗?

0 个答案:

没有答案