How to catch this error: "Notice: Undefined offset: 0"
You need to define your custom error handler like:
<?phpset_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";}
You can't with a try/catch block, as this is an error, not an exception.
Always tries offsets before using them:
if( isset( $a[ 0 ] ) { $b = $a[ 0 ]; }
I know it's 2016 but in case someone gets to this post.
You could use the array_key_exists($index, $array)
method in order to avoid the exception to happen.
$index = 99999;$array = [1,2,3,4,5,6];if(!array_key_exists($index, $array)){ //Throw myCustomException;}