How to catch this error: "Notice: Undefined offset: 0"

meotimdihia picture meotimdihia · Mar 21, 2011 · Viewed 84.7k times · Source

I want to catch this error:

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

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

Answer

zerkms picture zerkms · Mar 21, 2011

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