Programing

이 오류를 포착하는 방법 : "알림 : 정의되지 않은 오프셋 : 0"

lottogame 2020. 11. 11. 07:51
반응형

이 오류를 포착하는 방법 : "알림 : 정의되지 않은 오프셋 : 0"


이 오류를 포착하고 싶습니다.

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

편집 : 사실, 다음 줄에서이 오류가 발생했습니다.$parse = $xml->children[0]->children[0]->toArray();


다음과 같이 사용자 지정 오류 처리기를 정의해야합니다.

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

이것은 예외가 아니라 오류이기 때문에 try / catch 블록으로는 할 수 없습니다.

항상 오프셋을 사용하기 전에 시도합니다.

if( isset( $a[ 0 ] ) { $b = $a[ 0 ]; }

나는 그것이 2016 년이라는 것을 알고 있지만 누군가 가이 게시물을 보게 될 경우를 대비하여.

array_key_exists($index, $array)예외가 발생하지 않도록하기 위해 메서드를 사용할 수 있습니다 .

$index = 99999;
$array = [1,2,3,4,5,6];

if(!array_key_exists($index, $array))
{
    //Throw myCustomException;
}

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

또는 매우 일시적인 예외를 생성하는 비 효율성없이 :

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

일반적으로 try-catch 블록으로 알림을 포착 할 수 없습니다. 그러나 통지를 예외로 전환 할 수 있습니다! 다음과 같이 사용하십시오.

function get_notice($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();
    get_notice($codeOutput);
} catch (\Exception $exception) {
    // Catch (notice also)!
}

또한이 기능을 사용하여 경고를 포착 할 수 있습니다. 함수 이름을 get_warning으로 변경 "<b>Notice</b>:"하고 "<b>Warning</b>:".

Note: The function will catch an innocent output that contains:

<b>Notice</b>:

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

<b>Notice:</b>


Im sure why the Error Throw but i fix some..

in html2pdf.class.php

on Lines 2132:

//FIX:
$ctop=$corr[$y][$x][2]<=count($sw)?$corr[$y][$x][2]:count($sw);
$s = 0; for ($i=0; $i<$ctop; $i++) {$s+= array_key_exists($x+$i, $sw)? $sw[$x+$i]:0;}

SAME On line 2138:

//FIX:

$ctop=$corr[$y][$x][2]<=count($sw)?$corr[$y][$x][2]:count($sw);
 for ($i=0; $i<$ctop; $i++) {

the problem the array $sw not have a key of $corr[$y][$x][2] so i fix the loop for to max count($sw) to fix .. I dont know if that create an another consecuense but i resolve my problem y dont have any more errors..

So i hope works to you ..!!! Beats Reguards

참고URL : https://stackoverflow.com/questions/5373780/how-to-catch-this-error-notice-undefined-offset-0

반응형