采取以下功能的什么我想要做的一个例子:PHP:追加到抛出的异常信息
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();
}
}
我有嵌套try()
和catch()
语句。从最内部抛出异常并被捕获,然后执行一些函数并抛出另一个异常。注意我在哪写$e .=
,我明白这是不正确的语法。我想要做的是将字符串附加到例外的$e->getMessage()
。
我该怎么做呢?
+1不错,有很多清洁我的建议! – ManseUK 2012-01-06 10:24:22
感谢审查ManseUK。 @Milad Naseri,所有的代码应该尽可能的干净,尽可能的容易找到并扩展新的特性和功能。 – Rolice 2012-01-06 10:27:36
完全同意。 – 2012-01-06 11:05:21