Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 抛出一个接受(不是例外)错误吗?_Php_Exception_Design Patterns - Fatal编程技术网

Php 抛出一个接受(不是例外)错误吗?

Php 抛出一个接受(不是例外)错误吗?,php,exception,design-patterns,Php,Exception,Design Patterns,如果我们不仅可以抛出异常,还可以抛出接受呢 在这些情况下,你需要做一些事情,直到正确的事情完成。 示例:通过不同的方法查找url 通常,您会执行以下操作(在php类中): 为什么不: function setUrl(){ try{ $this->getUrlByRequest(); $this->getUrlBySomethingelse(); $this->makeUrl(); } catch(Acce

如果我们不仅可以抛出异常,还可以抛出接受呢

在这些情况下,你需要做一些事情,直到正确的事情完成。 示例:通过不同的方法查找url

通常,您会执行以下操作(在php类中):

为什么不:

function setUrl(){

    try{
        $this->getUrlByRequest();
        $this->getUrlBySomethingelse();
        $this->makeUrl();
    }
    catch(Acception $acc){
        return;
    }

    // still here, url not set
    throw new Exception('No url');
}

我不知道PHP如何处理异常,但在许多语言中,异常通常会带来相对较大的性能影响(与单个
if
相比)。因此,它们实际上应该仅用于特殊情况。
function setUrl(){

    try{
        $this->getUrlByRequest();
        $this->getUrlBySomethingelse();
        $this->makeUrl();
    }
    catch(Acception $acc){
        return;
    }

    // still here, url not set
    throw new Exception('No url');
}