meotimdihia
meotimdihia

Reputation: 4299

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

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();

Upvotes: 80

Views: 99225

Answers (6)

Caique Andrade
Caique Andrade

Reputation: 1362

For the people who are getting the error

PHP Notice: unserialize(): Error at offset 191 of 285 bytes in ...

and are getting the data from a database, Make sure that you have the database set the the correct encoding, I had the database set as latin1_swedish_ci and all of the data looked perfect, Infact when i copied it into a online unserialize it worked fine. I changed the collation to utf8mb4_unicode_ci and all worked fine.

Source User Contributed Notes: https://www.php.net/manual/pt_BR/function.unserialize.php

I tried to utf8_decode before unserialize, and it's works fine.

Piece of code where I applied utf8_decode()

Upvotes: 0

MAChitgarha
MAChitgarha

Reputation: 4288

Important: Prefer not using this method, use others. While it works, it is ugly and has its own pitfalls, like falling into the trap of converting real and meaningful output into exceptions.

Normally, you can't catch notices with a simple try-catch block. But there's a hacky and ugly way to do it:

function convert_notice_to_exception($output)
{
    if (($noticeStartPoint = \strpos($output, "<b>Notice</b>:")) !== false) {
        $position = $noticeStartPoint;
        for ($i = 0; $i < 3; $i++)
            $position = strpos($output, "</b>", $position) + 1;
        $noticeEndPoint = $position;
        $noticeLength = $noticeEndPoint + 3 - $noticeStartPoint;
        $noticeMessage = \substr($output, $noticeStartPoint, $noticeLength);

        throw new \Exception($noticeMessage);
    } else
        echo $output;
}

try {
    ob_start();
    // Codes here
    $codeOutput = ob_get_clean();

    convert_notice_to_exception($codeOutput);
} catch (\Exception $exception) {

}

Also, you can use this function for to catch warnings. Just change function name to convert_warning_to_exception and change "<b>Notice</b>:" to "<b>Warning</b>:".

Note: The function will catch normal output that contains:

<b>Notice</b>:

To escape from this problem, simply, change it to:

<b>Notice:</b>

Upvotes: 0

Luis Deras
Luis Deras

Reputation: 1269

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

Upvotes: 14

Prisoner
Prisoner

Reputation: 27628

$a[1] = 'jfksjfks';
try {
  $offset = 0;
  if(isset($a[$offset]))
    $b = $a[$offset];
  else
    throw new Exception("Notice: Undefined offset: ".$offset);
} catch (Exception $e) {
  echo $e->getMessage();
}

Or, without the inefficiency of creating a very temporary exception:

$a[1] = 'jfksjfks';
$offset = 0;
if(isset($a[$offset]))
  $b = $a[$offset];
else
  echo "Notice: Undefined offset: ".$offset;

Upvotes: 10

zerkms
zerkms

Reputation: 255005

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

Upvotes: 118

Macmade
Macmade

Reputation: 54000

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

Upvotes: 27

Related Questions