欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

PHP 高级编程(4/5)

程序员文章站 2022-05-21 11:30:47
...
SPL 提供了一系列标准异常。日常的使用中我们应该根据需求科学的使用它们,来使我们的程序更加健壮。LogicException 是从 Exception 基类派生的,没有添加任何附加方法。抛出逻辑异常和抛出标准一次的方法类似,区别在于逻辑异常须在应用程序编写有误时才抛出。下面演示下LogicException类的使用。
class App
{
    protected $_loaded = false;
    protected $_name;

    public function start()
    {
        $this->_loaded = true;
        $this->_name = 'unzin';
    }

    public function GetName()
    {
        if(!$this->_loaded)
        {
            throw new LogicException("Error Processing Request", 1);
        }
        return $this->_name;
    }
}

try
{
    $APP = new App();
    echo $APP -> GetName();
}
catch(LogicException $e)
{
    echo $e->getMessage();
}