11
Where it is possible to check all exceptions (native) that can be released by PHP? I searched and only found ways to treat with try/catch
.
11
Where it is possible to check all exceptions (native) that can be released by PHP? I searched and only found ways to treat with try/catch
.
13
As native exceptions sane:
Occurs when a callback refers to a function indefinite or if some arguments are missing.
Occurs when a callback refers to a method undefined 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 a length (length) is invalid.
Exception representing error in program logic. This type of exception must lead directly to a correction in your code.
Exception thrown when a value is not a valid key. This represents errors that cannot be detected at build time.
Exception thrown when an invalid index is prompted. This represents errors that must be detected at build time.
Exception thrown when adding an element to a container full.
Exception thrown to indicate interval errors (crease) during the execution of the program. Normally 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 runtime.
Exception thrown when performing an invalid operation on a container empty, 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.
13
Regarding PHP 7, the exceptions in common PHP errors were implemented, such as Warning
, Fatal Error
and Parse Error
.
Here is the list of errors that can be captured in PHP 7:
It’s some kind of mistake, like a Warning
or a Fatal Error
. It is the base class for all other errors that will be demonstrated below.
is launched when an error occurs during the execution of mathematical operations. In PHP 7.0, these errors include trying to run a Bitshift for a negative value, and any call to intDiv() that would result in a value outside the possible limits of an integer.
Derivative of ArithmeticError
, as its name says, it is launched when you try to do a split operation by 0.
It is thrown when an error occurs during PHP code analysis, for example, as in a eval
which is called. I believe another case is when you include another PHP script and in it there are errors.
This error is thrown when you have passed arguments to functions whose type is the unhealthy, 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 launch of the same when you an invalid number of arguments for a function.
It is thrown when a statement made by means of assert() returns false.
It seems to me that PHP has decided to treat the errors differently from the exceptions.
All these errors described above do not inherit the class Exception
traditional PHP. To capture them, you need to add the interface Throwable
in the option catch
.
Thus:
try{
$result = 59 / 0;
} catch (Throwable $e) {
var_dump($e instanceof DivisionByZeroError); // bool(true)
}
In PHP 7, both exceptions and errors implement the interface Throwable
.
6
Of the native exceptions, it is 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.
Browser other questions tagged php exception
You are not signed in. Login or sign up in order to post.
Good complement Wallacemaxters
– Ricardo