I saw this code in the PHP documentation:
try {
throw new ErrorException("Exception message", 0, E_USER_ERROR);
} catch(ErrorException $e) {
echo "This exception severity is: " . $e->getSeverity();
var_dump($e->getSeverity() === E_USER_ERROR);
}
And it continues:
This exception severity is: 256
bool(true)
What does exception severity mean, and do I have to use it at all?
The $severity
is an integer representing, well, the severity of the error thrown. The manual states that it can be any integer, but it's preferable to use a constant from the predefined error constants. These are the same used by error_reporting.
Notice that ErrorException
extends Exception
, adding the $severity
parameter. This is because ErrorException
is normally used to convert the normal errors PHP shows into Exception
s. This is done via set_error_handler().
So, the ErrorException::$severity
is really the severity of the PHP error that would have been shown if you hadn't thrown it as an Exception
. You can use it to decide what to do when you catch an ErrorException
depending on what caused it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With