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
orBadFunctionCallException
? -
Is there a defined pattern (a PSR or something like this) where do I explain when to use each one?
Reference PHP Manual: Exceptions