dougu5847 2012-01-06 10:16
浏览 84
已采纳

PHP:附加到抛出的异常消息

Take the following function as an example of what I want to do:

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();
    }
}

I have nested try() and catch() statements. An exception is thrown from the inner-most and is caught, I then perform some functions and throw another exception. Notice where I write $e .=, I understand this is the incorrect syntax. What I want to do is append the string to the exception's $e->getMessage().

How would I go about doing this?

  • 写回答

3条回答 默认 最新

  • donglan6777 2012-01-06 10:21
    关注

    Create you own exception class and create method for appending string to the message.

    <?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;
        }
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line