PHP: 'or' statement on instruction fail: how to throw a new exception?

后端 未结 7 1793
萌比男神i
萌比男神i 2020-12-06 04:30

Everyone here should know the \'or\' statemens, usually glued to an die() command:

$foo = bar() or die(\'Error: bar function return false.\');
7条回答
  •  甜味超标
    2020-12-06 05:12

    Why don't bar() and bb() throw the exceptions? In PHP Exceptions bubble up, so there's no need to throw the exception in the function/method where you call bar()/bb(). These exceptions may be thrown by bar()/bb(). In case you want to throw another exception, you can simply do:

    function foo() {
        try {
            $bar = bar();
        } catch (BarException) {
            throw new FooException;
        }
    }
    

提交回复
热议问题