Which Exception should I release according to each situation?

Asked

Viewed 189 times

7

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

BadFunctionCallException

BadMethodCallException

DomainException

InvalidArgumentException

LengthException

LogicException

OutOfBoundsException

OutOfRangeException

OverflowException

RangeException

RuntimeException

UnderflowException

UnexpectedValueException

The only one I really know to be different is ErrorException, allowing the exception to be made according to the data captured by set_error_handler.

However, as for the others, sometimes I feel a little confused about which 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;

}
  • I should cast what exception? InvalidArgumentException, UnexpectedValueException or BadFunctionCallException ?

  • Is there a defined pattern (a PSR or something like that) where it explains when to use each one?

Reference PHP Handbook: Exceptions

  • I didn’t understand the -1 :(

  • 2

    If PHP allowed it hinting type with primitive types, you wouldn’t even have to worry about that </rant>. But given your example, I think we should use InvalidArgumentException .

  • Now comes the question that tingles the brain: And when should I use UnexpcetedValueException and BadFunctionCallException ?

  • I’m trying to answer that. I do not know if the intention is to have description p/ all, I do not know if this would not leave the answer too long, especially if we take into account that the documentation is clear in all of them.

  • 3

    It is important to remember that the exceptions follow a hierarchy: http://php.net/manual/en/spl.exceptions.php#spl.exceptions.Tree

  • 1

    But, as you always say, documentation is rubbish!

  • 4

    Being a trash doesn’t mean she should be ignored.

  • kkkkkkkkkkkkkkkkkkkkk, ok

Show 3 more comments

2 answers

5


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

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

To BadFunctionCallException is used to indicate that the function the code is calling is not available at that time. How PHP is interpreted and fully dynamic 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 if it is clear and will post his doubt in some website for someone to settle for him. In this case it is even more important to have accurate information to better help those who do not even know the system he is trying to solve the problem.

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

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

It is not easy to say objectively when each one fits, it is 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 don’t even know how much this is actually respected by the community as something relevant.

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

  • very good. I would just like to understand this question of "...The function is not available at that time... PHP is interpreted and fully dynamic...". The function is not always available regardless of where it is declared (before or after the call, I say)?

  • 1

    No, in a language like PHP there are several situations where this is not true. Or because some project file is missing or because a PHP module is not loaded. I don’t know all the details because I’m from the time PHP had no exception :)

  • I just did a test. If you set a function that is inside a file included with include and call that function before that include, it will be deemed non-existent!

  • 1

    Yes, this is a very plausible situation, the interpreter doesn’t know it yet.

1

In accordance with this explanation, the exceptions derived from LogicException should be used to notify developers about logic issues in the application (by email or log).

The exceptions derived from RuntimeException should be captured to show some appropriate message to the user or try to circumvent the problem in some way.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.