vendredi 16 septembre 2016

Is throwing an Acception (not Exception) bad?

What if we could not only throw exceptions, but also Acceptions?

You have these situations , where you need to do something until the right thing is done. Example: find an url by different methods

Normally you would do something like (inside a php class):

function setUrl(){
    $this->setUrlByRequest();
    if($this->url){
        return;
    }
    $this->setUrlBySomethingelse();
    if($this->url){
        return;
    }
    $this->makeUrl();
    if(!$this->url){
        throw new Exception('no url');
    }
}

Why not:

function setUrl(){

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

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

Aucun commentaire:

Enregistrer un commentaire