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

phpnew Exception(string $message = "", int $code = 0, Throwable $previous = null);

Numune

Sadə bir istisna yaradaq ve onu emal edek:

php<?php
  try {
    throw new Exception('Something went wrong', 100);
  } catch (Exception $e) {
    echo 'Error: ' . $e->getMessage();
  }
?>

Kodun icra neticesi:

php'Error: Something went wrong'

Numune

Exception klassının əsas metodlarından istifade edek:

php<?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):

php'Message: Test exception
Code: 123
File: /path/to/file.php
Line: 3'

Numune

Istisna zamanı zeng stackini alaq:

php<?php
  function test() {
    throw new Exception('Stack trace test');
  }
  
  try {
    test();
  } catch (Exception $e) {
    print_r($e->getTrace());
  }
?>

Kodun icra neticesi (numune):

php[
  [
    'file' => '/path/to/file.php',
    'line' => 5,
    'function' => 'test',
    'args' => []
  ]
]

Hemçinin bax