Klas ErrorException
Klas ErrorException inarithi kutoka kwa klas ya msingi Exception na inatumika
kubadilisha makosa ya PHP kuwa uchaguo. Inaongeza kwa utendaji wa kawaida wa
uchaguo taarifa kuhusu ukali wa hitilafu (severity). Klas ni muhimu sana
wakati wa kutumia na kitendo set_error_handler.
Syntax
new ErrorException(
string $message = "",
int $code = 0,
int $severity = E_ERROR,
string $filename = __FILE__,
int $lineno = __LINE__,
Throwable $previous = null
);
Mfano
Tengeneza na chakata ErrorException:
<?php
try {
throw new ErrorException('Critical error', 0, E_ERROR);
} catch (ErrorException $e) {
echo 'Error: ' . $e->getMessage();
echo ' Severity: ' . $e->getSeverity();
}
?>
Matokeo ya utekelezaji wa kodi:
'Error: Critical error Severity: 1'
Mfano
Badilisha makosa ya kawaida ya PHP kuwa uchaguo:
<?php
function errorHandler($severity, $message, $file, $line) {
throw new ErrorException($message, 0, $severity, $file, $line);
}
set_error_handler('errorHandler');
try {
strpos(); // Idadi isiyo sahihi ya hoja
} catch (ErrorException $e) {
echo 'Caught exception: ' . $e->getMessage();
echo ' in ' . $e->getFile();
echo ' on line ' . $e->getLine();
}
?>
Matokeo ya utekelezaji wa kodi (mfano):
'Caught exception: strpos() expects at least 2 parameters, 0 given in /path/to/file.php on line 10'
Mfano
Pata taarifa kuhusu ukali wa hitilafu:
<?php
try {
throw new ErrorException('Warning message', 0, E_WARNING);
} catch (ErrorException $e) {
echo 'Severity level: ' . $e->getSeverity();
echo ' Is warning: ' . ($e->getSeverity() === E_WARNING ? 'yes' : 'no');
}
?>
Matokeo ya utekelezaji wa kodi:
'Severity level: 2 Is warning: yes'
Angalia pia
-
klas
Exception,
klas ya msingi kwa uchaguo wote katika PHP -
kitendo
set_error_handler,
kinachoweka kichakataji maalum cha makosa cha mtumiaji