tags:

views:

46

answers:

3
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 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

A: 

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

Reinis I.
+4  A: 

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.

Max
"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."Hmm... This is a custom exception already, meant to deal with EPP Exceptions only. So, normally, we would have a validation class for dealing with the common errors. If something passes the validation layer (can I call it like this?), both, client side, and server side, then, the exceptions will take place. Why should we prefer to have several places to deal with errors, instead of only 1? thks
MEM
My last comment IS a question. Not an strong assumption that will lead nowhere. :) I'm trying to better understand where should we use exceptions or other things... and why that's a better option.
MEM
@MEM please see my edit, I tried to tell you my view on when I´d use an exception and when its appropriate to resort to "normal" validation/control flow.
Max
@Max - Thanks a lot for your clarification.
MEM
+1  A: 

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");
}
kali
Do you see "EppCommandsExceptions"? There I'm extending the Exception class, so all EppCommand Errors I would like to pass by this exception handler (if I may call so). At this time, yes, it's only a question of showing the messages to the user.
MEM