ErrorException အတန်း

အတန်း ErrorException သည် အခြေခံအတန်း Exception မှ အမွေဆက်ခံပြီး PHP အမှားများကို ချွင်းချက်များအဖြစ် ပြောင်းလဲရန် အသုံးပြုပါသည်။ ၎င်းသည် စံချွင်းချက်လုပ်ဆောင်ချက်များအား အမှား၏ ပြင်းထန်မှု (severity) အချက်အလက်ကို ပေါင်းထည့်ပေးပါသည်။ ဤအတန်းသည် အထူးသဖြင့် set_error_handler လုပ်ဆောင်ချက်နှင့် အသုံးပြုသောအခါ အသုံးဝင်ပါသည်။

ဝါကျဖွဲ့ထုံး

phpnew ErrorException(
  string $message = "",
  int $code = 0,
  int $severity = E_ERROR,
  string $filename = __FILE__,
  int $lineno = __LINE__,
  Throwable $previous = null
);

ဥပမာ

ErrorException ကို ဖန်တီးပြီး ကိုင်တွယ်ကြပါစို့။

php<?php
  try {
    throw new ErrorException('Critical error', 0, E_ERROR);
  } catch (ErrorException $e) {
    echo 'Error: ' . $e->getMessage();
    echo ' Severity: ' . $e->getSeverity();
  }
?>

ကုဒ် Run ပြီးစီးမှု ရလဒ်။

php'Error: Critical error Severity: 1'

ဥပမာ

PHP ၏ စံအမှားများကို ချွင်းချက်များအဖြစ် ပြောင်းလဲကြပါစို့။

php<?php
  function errorHandler($severity, $message, $file, $line) {
    throw new ErrorException($message, 0, $severity, $file, $line);
  }
  
  set_error_handler('errorHandler');
  
  try {
    strpos(); // အငြင်းအခုံ အရေအတွက် မမှန်ကန်ပါ
  } catch (ErrorException $e) {
    echo 'Caught exception: ' . $e->getMessage();
    echo ' in ' . $e->getFile();
    echo ' on line ' . $e->getLine();
  }
?>

ကုဒ် Run ပြီးစီးမှု ရလဒ် (ဥပမာ)။

php'Caught exception: strpos() expects at least 2 parameters, 0 given in /path/to/file.php on line 10'

ဥပမာ

အမှား၏ ပြင်းထန်မှု အချက်အလက်ကို ရယူကြပါစို့။

php<?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');
  }
?>

ကုဒ် Run ပြီးစီးမှု ရလဒ်။

php'Severity level: 2 Is warning: yes'

ဆက်လက်ဖတ်ရှုရန်

  • အတန်း Exception,
    PHP ရှိ ချွင်းချက်အားလုံးအတွက် အခြေခံအတန်း
  • လုပ်ဆောင်ချက် set_error_handler,
    အသုံးပြုသူသတ်မှတ် အမှားကိုင်တွယ်ရေးကို သတ်မှတ်ပေးသည်