如何捕获此错误:“注意:未定义偏移量:0"

2021-12-26 00:00:00 error-handling php try-catch

我想捕捉这个错误:

$a[1] = 'jfksjfks';
try {
      $b = $a[0];
} catch (Exception $e) {
      echo "jsdlkjflsjfkjl";
}

实际上,我在以下行中收到此错误:$parse = $xml->children[0]->children[0]->toArray();

in fact, I got this error on the following line: $parse = $xml->children[0]->children[0]->toArray();

推荐答案

您需要定义您的自定义错误处理程序,例如:

You need to define your custom error handler like:

<?php

set_error_handler('exceptions_error_handler');

function exceptions_error_handler($severity, $message, $filename, $lineno) {
  if (error_reporting() == 0) {
    return;
  }
  if (error_reporting() & $severity) {
    throw new ErrorException($message, 0, $severity, $filename, $lineno);
  }
}

$a[1] = 'jfksjfks';
try {
      $b = $a[0];
} catch (Exception $e) {
      echo "jsdlkjflsjfkjl";
}

相关文章