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