Geo*_*ith 1 php exception-handling exception
以下功能为例,说明我想做的事情:
public function save() {
$this->connect('wb');
try {
if(!$this->lock())
throw new Exception("Unable to acquire configuration locks");
if (!$backup = $this->backup())
throw new Exception("Failed to create configuration backup");
try {
if(!fwrite($this->_pointer, $this->dump("string")));
throw new Exception("Error occured while writing to configuration");
$this->unlock();
$this->disconnect();
} catch (Exception $e) {
if(rename ($backup, $this->_file))
$e .= PHP_EOL."Successfully restored configuration from backup";
else
$e .= PHP_EOL."Failed to restore configuration from backup";
$this->unlock();
$this->disconnect();
throw $e;
}
} catch (Exception $e) {
echo PHP_EOL, $e->getMessage();
}
}
Run Code Online (Sandbox Code Playgroud)
我有嵌套try()和catch()声明.从最内层抛出一个异常并被捕获,然后我执行一些函数并抛出另一个异常.注意我写的地方$e .=,我明白这是不正确的语法.我想要做的是将字符串追加到异常中$e->getMessage().
我该怎么做呢?
创建自己的异常类并创建用于将字符串附加到消息的方法.
<?php
class SuperException extends Exception
{
public function AppendToMessage($msg)
{
// $this->message is inherited from Exception class,
// where it is protected field (member) of the class
$this->message .= $msg;
}
}
?>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2389 次 |
| 最近记录: |