Exception klassy

Exception klassy PHP-de ähli düşündirisler üçin esasy klasdyr. Ol düşündirisler bilen işlemek üçin esasy usullary öz içine alýar: ýalňyşlyk baradaky habar almak, ýalňyşlyk kody, düşündirisiň döredilen faýly we setiri, şeýle-de çagyryş stekini. Düşündirisi döredilende, habar, ýalňyşlyk kody we öňki düşündirisi bermek mümkin.

Sintaksis

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

Mysal

Sadaky düşündirisi döredeliň we işleliň:

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

Kodyň ýerine ýetiriliş netijesi:

php'Error: Something went wrong'

Mysal

Exception klasynyň esasy usullaryny ulanýarys:

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";
  }
?>

Kodyň ýerine ýetiriliş netijesi (mysal):

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

Mysal

Düşündiri wagty çagyryş stekini alalyň:

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

Kodyň ýerine ýetiriliş netijesi (mysal):

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

Şeýle-de görüň

  • ErrorException klasy,
    ýalňyşlyklary düşündirisi görnüşinde üpjün edýär
  • set_exception_handler funksiýasy,
    ulanyjy tarapyndan düşündiris işleýjisini sazlaýar