fab*_*bio 2 php exception-handling exception instance-variables throw
我们通常在实例化时设置消息,如下所示:
throw new Exception($msg);`
Run Code Online (Sandbox Code Playgroud)
但我处于一种情况,我有一个存储在实例变量中的默认异常对象,并在对象生命周期中使用它,如下所示:
throw $this->my_exception;
Run Code Online (Sandbox Code Playgroud)
由于我正在重用同一个对象,我需要能够在抛出异常之前随时设置消息,你知道吗?
创建自定义例外:
class MyException extends Exception
{
public function setMessage($message) {
$this->message = $message;
}
}
Run Code Online (Sandbox Code Playgroud)
然后你可以创建并抛出此异常
$this->exception = new MyException;
// ...
$this->exception->setMessage('Bad stuff happened');
throw $this->expection;
Run Code Online (Sandbox Code Playgroud)
虽然我老实说不明白为什么你会做那样的事情.
归档时间: |
|
查看次数: |
1917 次 |
最近记录: |