Where can I check all the exceptions (native) that can be launched by PHP? I searched and found only forms of treatment with try/catch
.
Where can I check all the exceptions (native) that can be launched by PHP? I searched and found only forms of treatment with try/catch
.
native exceptions are:
Occurs when a callback refers to an undefined function or if some arguments are missing.
Occurs when a callback refers to an method that is not defined or if some arguments are missing.
Exception thrown when a value does not adhere to a valid data domain.
Exception thrown if an argument is not of the expected type.
Exception thrown if length ( length ) is invalid.
Exception representing error in program logic. This type of exception should lead directly to a fix in your code.
Exception thrown when a value is not a valid key. This represents errors that can not be detected at compile time.
Exception thrown when an invalid index was requested. This represents errors that should be detected at compile time.
Exception thrown when adding an element to a container full.
Exception thrown to indicate interval errors ( range ) during program execution. Usually this means that there was an arithmetic error except under / overflow . This is the runtime version of DomainException .
Exception thrown if an error occurs that can only be found at run time.
Exception thrown when performing an invalid operation on an empty container , such as removing an element.
Exception thrown when a value does not match a set of values. Usually, this happens when a function calls another function and expects the return value to be of a certain type.
Regarding PHP 7, exceptions have been implemented in common PHP errors such as Warning
, Fatal Error
and Parse Error
.
Here's the list of errors that can be caught in PHP 7:
It's an error, such as Warning
or Fatal Error
. It is the base class for all other errors that will be demonstrated next.
is thrown when an error occurs while performing math operations. In PHP 7.0, these errors include trying to execute a BitShift by a negative value, and any call to intDiv () that would result in a value outside the possible bounds of an integer.
Derived from ArithmeticError
, as the name itself says, is thrown when you try to do a split operation by 0.
It is thrown when an error occurs during parsing PHP code, for example, as in a eval
that is called. I think another case is when you include another PHP script and there are errors in it.
This error is thrown when you pass arguments to functions whose type is inspected, when the return is different from what is defined in the function. In the manual it also says that in strict mode there is the release of the same when you an invalid number of arguments to a function.
It is thrown when a statement made by assert () returns false.
It seems to me that PHP resolved to treat errors differently from exceptions.
All of these errors described above do not inherit the traditional Exception
class from PHP. To capture them, you must add the Throwable
interface in the catch
option.
So:
try{
$result = 59 / 0;
} catch (Throwable $e) {
var_dump($e instanceof DivisionByZeroError); // bool(true)
}
In PHP 7, both exceptions and errors implement the Throwable
interface.
Of the native exceptions, it's good to know that some extend others, forming the next hierarchy :
It is interesting to create more specific exceptions in the domain of your application, extending the existing exceptions.
<?php
class QueroBatataException extends UnexpectedValueException { }
class QueroCebolaException extends UnexpectedValueException { }
function batata($nomeLegume) {
if ($nomeLegume !== 'batata') {
throw new QueroBatataException('Me dê batata');
}
echo 'Huuun Batata!' . PHP_EOL;
}
function cebola($nomeLegume) {
if ($nomeLegume !== 'cebola') {
throw new QueroCebolaException('Me dê cebola');
}
echo 'Huuun Cebola!' . PHP_EOL;
}
So in your code, you can do this:
<?php
$legume = 'batata';
try {
batata($legume);
cebola($legume);
} catch (UnexpectedValueException $e) {
echo $e->getMessage();
}
// Saída:
// Huuun Batata!
// Me dê cebola
See the running code .