I am using ErrorException as my central error handling function for Errors and Exception:
Example:
set_error_handler('my_error_handler');
set_exception_handler('my_exception_handler');
function my_error_handler ($errno, $errstr, $errfile, $errline)
{
my_exception_handler(new ErrorException($errstr, 0, $errno, $errfile, $errline));
}
function my_exception_handler ($e)
{
echo $e->getSeverity();
echo $e->getCode();
echo $e->getMessage();
}
Now it appears $e->getSeverity(); is only present in ErrorException and not in the default Exception;
Because when I throw new Exception('test'); I get a Fatal error: Call to undefined method Exception::getSeverity().
But when I throw new ErrorException('test'); it works as expected.
This seems logical but is there a way I can still use throw new Exception('test'); without getting the Fatal Error? Most third-party libraries use Exception (not ErrorException) so it will be problematic when a third-party exception occurs, it will cause Fatal Error. How can I solve this?
You can check if the caught exception is an
ErrorException: