What exception should I post according to each situation?

5

In PHP, we have several types of exceptions that can be thrown. Among them:

BadFunctionCallException

BadMethodCallException

DomainException

InvalidArgumentException

LengthException

LogicException

OutOfBoundsException

OutOfRangeException

OverflowException

RangeException

RuntimeException

UnderflowException

UnexpectedValueException

The only thing I really know about is the ErrorException , which lets you throw the exception according to the data captured by set_error_handler .

However, as for the others, sometimes I feel a bit confused about which one to use.

For example:

function teste($int, array $array){

   if (! is_int($int)) {
     // lanço minha exceção aqui por que o número não é do tipo INT
   }

   array_push($array, $int);
   return $array;

}
  • Should I throw an exception? InvalidArgumentException , UnexpectedValueException or BadFunctionCallException ?

  • Is there a defined pattern (a PSR or something like this) where do I explain when to use each one?

Reference PHP Manual: Exceptions

    
asked by anonymous 09.02.2015 / 19:29

2 answers

5

In this example it seems clear to me that the correct exception is InvalidArgumentException . The intent of this exception is to indicate that a wrong argument was passed to the parameter . The documentation in the link provided in the question shows this.

The UnexpectedValueException is used when the same problem occurs in the return of a type when calling a function.

The BadFunctionCallException is used to indicate that the function that the code is calling is not available at that time. As PHP is interpreted and fully dynamic this is possible.

It is important to use the right exceptions to document precisely what is happening. A programmer who understands all the nuances will benefit from this. Of course for many programmers it does not make much difference, he does not understand the error even though he is clear and will post his question on some site for someone to solve for it. In this case it is even more important to have accurate information to better assist those who do not even know the system as it is trying to solve the problem.

All these exceptions have documentation and a careful reading indicates where each one should be used.

In some cases the correct exception is the one you create. You should not always use a generic language exception. Nor should you also create new exceptions if an existing one fits well.

It's not easy to say objectively when it fits each, it's a matter of analyzing the situation and using common sense, experience, interpreting the documentation correctly.

There are few PSRs and as far as I know there are none. I do not know how much this is actually respected by the community as something relevant.

Exception is a subject that draws me a lot because almost everyone abuses them. I've written when creating and using exceptions in C # . I know the PHP culture is different and some things do not fit there for other languages but I think it helps a little.

    
09.02.2015 / 19:48
0

As this explanation , the derived exceptions of LogicException should be used to notify developers of application logic issues (via email or log).

Exceptions derived from RuntimeException must be captured to show some appropriate message to the user or try to work around the problem in some way.

    
15.08.2016 / 13:51