没有任何消息我可以使用throw吗?

Sha*_*deh 2 php exception-handling try-catch

这是我的代码:

try {

    if ( condition 1 ) {
        throw;
    } else {
        // do something
    }

    // some code here

    if ( condition 2 ){
        throw;
    }

} catch (Exception $e) {
    echo "something is wrong";
}
Run Code Online (Sandbox Code Playgroud)

如您所见,我的catch块有自己的错误消息,并且该消息是常量.所以当我这样使用时,我真的不需要传递消息throw:

throw new Exception('error message');
Run Code Online (Sandbox Code Playgroud)

我可以throw没有任何东西使用吗?我只需要跳进catch阻止.

老实说写一个无用的错误信息对我来说很烦人.


如您所知,我当前的代码有语法错误:( 指的是throw;)

解析错误:语法错误,意外';' 在{path}

zer*_*kms 7

message参数在Exception构造函数中是可选的.所以,如果你没有/想要放 - 只是不要:

throw new Exception;
Run Code Online (Sandbox Code Playgroud)

但是你仍然必须抛出Exception类的实例(或者扩展它的类),因为它是php语言语法的一部分.