2012-01-06 16 views
1

采取以下功能的什么我想要做的一个例子: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()

我该怎么做呢?

回答

7

创建您自己的异常类并创建将字符串附加到消息的方法。

<?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; 
    } 
} 
?> 
+1

+1不错,有很多清洁我的建议! – ManseUK 2012-01-06 10:24:22

+1

感谢审查ManseUK。 @Milad Naseri,所有的代码应该尽可能的干净,尽可能的容易找到并扩展新的特性和功能。 – Rolice 2012-01-06 10:27:36

+0

完全同意。 – 2012-01-06 11:05:21

0

为什么不使用单独的变量来保留消息?

public function save() { 
    $this->connect('wb'); 
    $exceptionMessage = ""; 
    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)) 
       $exceptionMessage .= PHP_EOL."Successfully restored configuration from backup"; 
      else 
       $exceptionMessage .= PHP_EOL."Failed to restore configuration from backup"; 
      $this->unlock(); 
      $this->disconnect(); 
      throw $e; 
     } 
    } catch (Exception $e) { 
     echo PHP_EOL. $exceptionMessage . PHP_EOL . $e->getMessage(); 
    } 
} 
1

大多数支持异常处理的面向对象的语言框架实现了对这个主题的需求。您需要了解例外不是日志发生了什么事。相反,他们在那里准确地查明错误发生的位置。

因此,不仅该语法不正确,整个想法违反了一打OOD原则。您应该引入一个记录器类,随时收集有关错误的信息,并使用例外来确定需要记录的事件

class ErrorLogger { 

    private $log; 

    public function __construct() { 
     $this->log = array(); 
    } 

    public function log(Exception $e) { 
     array_push($this->log, $e->getMessage()); 
    } 

} 

,并进一步在代码:

$logger = new ErrorLogger(); 
try { 
    : 
} catch (Exception $e) { 
    $logger->log($e); 
} 
+0

感谢您的回复,我打算仅向用户显示消息并向更高级别用户显示详细信息,但我不同意。我的例外的主要目的是执行错误特定的逻辑。我不相信这是违反OOD原则的。如果我的一个用户看到一个详细的错误,他们会被吓倒并离开,如果他们看到“发生数据库错误”,那么他们不太可能会受到惊吓。 – 2012-01-06 10:53:02

+0

我的记录器在这里只执行一个示例逻辑。您自己的实现可以使用两种实现的接口:一个用于高级用户,另一个用于最终用户。 – 2012-01-06 10:59:52

+0

不要误解我的意思我不是在敲你的记录器。我认为这是一个好主意。我将显示一个简单的异常消息,并使用我自己的异常类登录到服务器上的错误文件。 – 2012-01-06 11:01:45