Exception Klassı
Exception klassı PHP-də butun istisnalar üçün əsas klassı temsil edir.
O, istisnalarla iş üçün əsas metodları ehtiva edir: xeta mesajının alınması,
xeta kodu, istisnanın yarandığı fayl ve sətir, hemçinin zeng stacki.
Istisna yaradarken mesaj, xeta kodu ve evvelki istisna ötürüle biler.
Sintaksis
new Exception(string $message = "", int $code = 0, Throwable $previous = null);
Numune
Sadə bir istisna yaradaq ve onu emal edek:
<?php
try {
throw new Exception('Something went wrong', 100);
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}
?>
Kodun icra neticesi:
'Error: Something went wrong'
Numune
Exception klassının əsas metodlarından istifade edek:
<?php
try {
throw new Exception('Test exception', 123);
} catch (Exception $e) {
echo 'Message: ' . $e->getMessage() . "\n";
echo 'Code: ' . $e->getCode() . "\n";
echo 'File: ' . $e->getFile() . "\n";
echo 'Line: ' . $e->getLine() . "\n";
}
?>
Kodun icra neticesi (numune):
'Message: Test exception
Code: 123
File: /path/to/file.php
Line: 3'
Numune
Istisna zamanı zeng stackini alaq:
<?php
function test() {
throw new Exception('Stack trace test');
}
try {
test();
} catch (Exception $e) {
print_r($e->getTrace());
}
?>
Kodun icra neticesi (numune):
[
[
'file' => '/path/to/file.php',
'line' => 5,
'function' => 'test',
'args' => []
]
]
Hemçinin bax
-
klass
ErrorException,
ki, xetaları istisna kimi temsil edir -
funksiyanı
set_exception_handler,
ki, istifadeçi istisna emaledicisini teyin edir