开发者

php exceptions - can we have multiple throws?

    if ($disponivel === 0)
    {
        $razao = $check->cd->reason;
        $mensagem = "the domain isn't available. Reason: ".$razao;
    }
    elseif($disponivel === 1)
    {
        $mensagem = "the domain doesn't exist - free to register.";
    }

    return $mensagem;
}
else
{
    throw new EppCommandsExceptions('Domain isn't supported - '.$result->msg, $codigo);
}

Do you see those $mensagem strings? They are also error messages and my question is, instead of having $mensagem开发者_C百科 displaying some error messages, can we use several throw exceptions instead?

Update: I DO NOT mean to throw the exceptions all at once. Each exception at his time.

Thanks in advance, MEM


You can't throw multiple, but since PHP 5.3 you can supply previous to Exception's constructor to create a linked list of exceptions.

For example, here's a 3-item chain:

$a = new Exception('Exception a');
$b = new Exception('Exception b', 0, $a);
throw new Exception('Exception c', 0, $b);

Then, in your exception handler, you can traverse through the chain with getPrevious

do {
    printf("%s:%d %s (%d) [%s]\n", $e->getFile(), $e->getLine(), $e->getMessage(), $e->getCode(), get_class($e));
} while($e = $e->getPrevious());


You mean something like

else {
    throw new XException(...);
    throw new YException(...);
    throw new ZException(...);
}

... and all of them are thrown "at once"?

No, thats not possible and wouldn´t make too much sense imho. How should the client code catching these exceptions look?

Furthermore you shouldn´t use exceptions as a replacement for normal flow control structures, exceptions should only handle, well, exceptional errors, like not being able to connect to a database etc.

You could implement another custom exception class which takes an array of errors as an argument, and the client code can do things like:

catch(SomeException $e) {
    $messages = $e->getErrorMessages();
}

As I don´t speak the language your language, I cant really tell what you are trying to do in that code bit you posted, otherwise I could suggest something more specific.


EDIT/UPDATE:

@MEM thanks for updating your code with english error messages. Are you implementing something like a domain registration service?

Of course, this is a bit of a difficult topic as everbody has his preferences, but I wouldn´t throw an exception if e. g. the user tried to register a domain thats already been used by someone else. This is not an exceptional state, this is to be excepted. I would make a validation class/method which collects these error messages which in turn get displayed to the user.

When would I throw an exception in an app like yours? I don´t know much about domain registration, but if you are fetching the info whether a domain is free or not from a remote server/webservice and this webservice is down, then I would throw an exception. It gets caught in the Controller (I image a MVC web app) which in turn replies to the client with a "Server down, please try again later" message.


you can throw and catch different exceptions.

If all you want to do is print the error, you can throw same type of exceptions aswell. But I'm going to assume you need different approach to each case

function myFunction() {
  if($a) {
    throw new AException('A Error');
  } else if($b) {
    throw new BException('B Error');
  } else if($c) {
    throw new CExceptıon('C Error');
  }
}

try {
  myFunctıon();
} catch (AException $aException) {
  echo $aException->getMessage();
} catch (BException $bException) {
  echo "this is a terrible case, don't do that again pls";
  $mydbobject->rollback();
} catch (CException $cException) {
  mailDevTeam("the server made a boo boo");
}


Of course, just subclass the base exception for every message.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜