首先抱歉我的英文...
我有关于抛出异常的问题;请看这段代码:
第一
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方法并在需要时抛出异常?