Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Exception severity in PHP?

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?

like image 877
Michael Akinlaby Avatar asked Oct 30 '22 02:10

Michael Akinlaby


1 Answers

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 Exceptions. 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.

like image 79
ishegg Avatar answered Nov 15 '22 07:11

ishegg