PHP在哪里抛出异常

时间:2016-06-28 17:52:45

标签: php exception

首先抱歉我的英文...

我有关于抛出异常的问题;请看这段代码:

第一

class SimpleClass
{
    public function mainFn()
    {
        $this->notTheMainFn1();
        $this->notTheMainFn2();
    }

    private function notTheMainFn1()
    {
        // fn returns value or throw Exception
    }

    private function notTheMainFn2()
    {
        // fn returns value or throw Exception
    }
}

第二

class SimpleClass2
{
    public function mainFn()
    {
        if ($this->notTheMainFn() === false) {
            throw new Exception('some error');
        }
    }

    private function notTheMainFn()
    {
        // fn returns value or false if error
    }
}

哪种方式更好?最好在depend方法中返回false或null,并检查main方法并在需要时抛出异常?

0 个答案:

没有答案