Reputation: 74018
PHP 5.1 has introduced ErrorException. The constructor of the two functions differs
public __construct ([ string $message = "" [, int $code = 0 [, Exception $previous = NULL ]]] )
public __construct ([ string $message = "" [, int $code = 0 [, int $severity = 1 [, string $filename = __FILE__ [, int $lineno = __LINE__ [, Exception $previous = NULL ]]]]]] )
Is there a difference when to use either?
I suspect that the above use case is incorrect:
<?php
class Data {
public function save () {
try {
// do something
} catch (\PDOException $e) {
if ($e->getCode() == '23000') {
throw new Data_Exception('Foo Bar', $e);
}
throw $e
}
}
}
class Data_Exception extends ErrorException /* This should not be using ErrorException */ {}
It isn't documented well, but it appears that ErrorException
is designed to be used explicitly from the custom error handler as in the original example, http://php.net/manual/en/class.errorexception.php.
function exception_error_handler($errno, $errstr, $errfile, $errline ) {
throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
}
set_error_handler("exception_error_handler");
Upvotes: 19
Views: 12833
Reputation: 14948
ErrorException
is mostly used to convert php error (raised by error_reporting) to Exception
.
You should avoid using directly Exception
which is too wide. Subclass it with specific Exception
or use predefined SPL Exception
To follow your edit : Yes extends Exception
rather than ErrorException
.
Upvotes: 23