首頁 > 後端開發 > PHP7 > 主體

聊聊在PHP7對於Error的處理是怎樣的

藏色散人
發布: 2023-02-18 08:46:02
轉載
2229 人瀏覽過

前段時間在專案中遇到當時覺得比較奇怪的情況:使用 GuzzleHttp 發送 curl 請求,API 回應逾時導致拋出例外。但 catch(\Exception) 並沒有捕獲異常,導致程式碼意外停止運作。後來查資料發現,在 PHP 7 中,GuzzleHttp 請求逾時拋出的異常繼承的是 Error,而 Error 並沒有繼承 Exception,所以 catch(\Exception) 無法捕獲並處理該異常。

PHP 7 中對 Error 的處理

在 PHP 5 中,當程式中有致命錯誤發生時,腳本會立即停止執行。並且,透過 set_error_handler 設定的錯誤處理程序在這種情況下並不會被呼叫。

【推薦學習:PHP7教學

⒈ 自訂錯誤處理程式set_error_handler

  set_error_handler 接受兩個參數,第一個為自訂的錯誤處理函數,第二個參數指定觸發該自訂錯誤處理函數的錯誤等級。但需要指出的是,在任何時候,只能有一個自訂的錯誤處理程序運作。

function func_notice($num, $str, $file, $line) {
    print "Encountered notice $num in $file, line $line: $str\n";
}
function func_error($num, $str, $file, $line) {
    print "Encountered error $num in $file, line $line: $str\n";
}
set_error_handler("func_notice", E_NOTICE);
set_error_handler("func_error", E_ERROR);
echo $foo;
登入後複製

  以上程式碼執行以後,會輸出 PHP Notice: Undefined variable: foo 。在第二個 set_error_handler 執行後,自訂錯誤處理函數變成了 func_error ,同時,觸發自訂錯誤處理函數的錯誤等級變成了 E_ERROR 。而在 PHP 中,變數未定義只會觸發 E_NOTICE 等級的錯誤,所以自訂的錯誤處理函數並不會被觸發。

需要指出的是,自訂的錯誤處理函數對以下幾種錯誤等級並不起作用:

E_ERROR、E_PARSE、E_CORE_ERROR、E_CORE_WARNING、E_COMPILE_ERROR、E_COMPILE_WARNING、E_STRICT
登入後複製

  在上述幾種自訂錯誤處理程序無法處理的錯誤中,凡是以ERROR 結尾的都是致命錯誤。其他幾種雖然不是致命錯誤,但

  • E_PARSE 是在解析PHP 程式碼時產生的錯誤,此時PHP 程式碼尚未開始運行,自訂錯誤處理程序自然無法處理該錯誤

  • E_CORE_WARNING 產生於PHP 的初始化啟動階段,此時PHP 程式碼仍未執行,所以無法被自訂錯誤處理程序處理

  • # E_COMPILE_WARNING 是在PHP 程式碼的編譯階段產生,所以不能被自訂錯誤處理程序處理

而至於E_STRICT 是PHP 為了保證程式碼的最佳互通性和向前相容而提出的程式碼修改建議,自然也不會被自訂錯誤處理函數處理

function func_error($num, $str, $file, $line) {
    print "Encountered error $num in $file, line $line: $str\n";
}
set_error_handler('func_error', E_NOTICE);
$obj = 'foo';
$obj->method();
登入後複製

   以上程式碼運行輸出結果:

PHP Fatal error:  Call to a member function method() on string
登入後複製

  雖然設定了自訂錯誤處理程序,但在致命錯誤發生時,並不起作用。

  對於這種自訂錯誤處理程序無法處理的致命錯誤,在PHP 5 中可以透過註冊一個終止回調(shutdown_function)來記錄具體的錯誤訊息,但也僅限於記錄錯誤訊息,當發生致命錯誤時程式碼仍然會停止運作。

$shutdownHandler = function(){
    print PHP_EOL;
    print "============================" . PHP_EOL;
    print "Running the shutdown handler" . PHP_EOL;
    $error = error_get_last();
    if (!empty($error))
    {
        print "Looks like there was an error: " . print_r($error, true) . PHP_EOL;
        // 可以添加记录日志的逻辑
    }
    else
    {
        // 程序正常运行结束
        print "Running a normal shutdown without error." . PHP_EOL;
    }
};
register_shutdown_function($shutdownHandler);
$obj = 'foo';
$obj->method();
登入後複製

  以上程式碼執行會輸出

PHP Fatal error:  Call to a member function method() on string in /home/chenyan/test.php on line 24
============================
Running the shutdown handler
Looks like there was an error: Array
(
    [type] => 1
    [message] => Call to a member function method() on string
    [file] => /home/chenyan/test.php
    [line] => 24
)
登入後複製

#⒉ 撤銷自訂錯誤處理程序

  當同時設定多個自訂錯誤處理程序時,雖然只有最後設定的自訂錯誤處理程序起作用。但所有設定的自訂錯誤處理程序會以堆疊的方式儲存(FILO)。

  使用 restore_error_handler 可以撤銷最近一次設定的自訂錯誤處理程序;如果同時呼叫了多次 set_error_handler ,則每呼叫一次 restore_error_handler,處於堆疊頂端的錯誤處理程序就會被撤銷。

function func_notice($num, $str, $file, $line) {
    print "Encountered notice : $str\n";
}
set_error_handler("func_notice", E_NOTICE);
set_error_handler("func_notice", E_NOTICE);
set_error_handler("func_notice", E_NOTICE);
echo $foo;
set_error_handler("func_notice", E_NOTICE);
echo $foo;
restore_error_handler();
echo $foo;
restore_error_handler();
echo $foo;
restore_error_handler();
echo $foo;
restore_error_handler();
echo $foo;
登入後複製

  以上程式碼運行,會輸出:

Encountered notice : Undefined variable: foo
Encountered notice : Undefined variable: foo
Encountered notice : Undefined variable: foo
Encountered notice : Undefined variable: foo
Encountered notice : Undefined variable: foo
PHP Notice:  Undefined variable: foo
登入後複製

⒊ PHP 7 中對錯誤的處理

  在PHP 7 中,當有致命錯誤或E_RECOVERABLE_ERROR 類型的錯誤發生時,通常會拋出一個Error,程式並不會終止。

try {
    $obj = 'foo';
    $obj->method();
} catch (\Error $e) {
    echo $e->getMessage();
}
登入後複製

  運行以上程式碼會輸出

Call to a member function method() on string
登入後複製

E_RECOVERABLE_ERROR 是一種可捕獲的致命錯誤,這種錯誤的出現並不會使得Zend 引擎處於不穩定的狀態,但必須被捕獲並且處理。如果不處理,那麼這種錯誤最終會變成 E_ERROR 類型的錯誤,最終導致 PHP 程式碼停止運行。在

  php 7 中,並不是所有的致命錯誤都會拋出 Error,在某些特定情況下出現的致命錯誤( Out Of Memory)仍然會導致程式碼停止運作。另外,如果拋出的 Error 沒有被捕獲並處理,則程式碼仍然會停止運行。

// bak.sql 的大小为 377 M
// PHP 配置的 memory_limit = 128M
try {
    $file = './bak.sql';
    file_get_contents($file);
} catch (\Error $e) {
    echo $e->getMessage();
}
// 执行以上代码,仍然会产生致命错误
PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 395191240 bytes)
// 抛出的 Error 没有被捕获并处理,代码依然会停止运行
$obj = 'foo';
$obj->method();
// 执行以上代码,由于并没有用 try/catch 捕获并处理抛出的 Error,程序仍然会停止运行
PHP Fatal error:  Uncaught Error: Call to a member function method() on string
登入後複製

  PHP 7 中的 Error 並沒有繼承 Exception,之所以這樣做是為了防止 PHP 5 中捕獲並處理 Exception 的程式碼捕獲這些 Error。因為在 PHP 5 中,這些致命錯誤是會導致程式碼停止運作的。

  Error 和 Exception 都繼承自 Throwable 。在 PHP 7 中,Throwable 是一個 interface,所有能透過 throw 關鍵字拋出的物件都實作了這個 interface。

interface Throwable
{
    public function getMessage(): string;
    public function getCode(): int;
    public function getFile(): string;
    public function getLine(): int;
    public function getTrace(): array;
    public function getTraceAsString(): string;
    public function getPrevious(): Throwable;
    public function __toString(): string;
}
登入後複製

  需要指出的是,Throwable 是 PHP 底层的 interface,PHP 代码中不能直接实现 Throwable 。之所以作出这个限制,是因为通常只有 Error 和 Exception 可以被抛出,并且这些抛出的 Error 和 Exception 中还存储了它们被抛出的堆栈跟踪信息,而 PHP 代码中开发者自定义的 class 无法实现这些。

  要在 PHP 代码中实现 Throwable 必须通过继承 Exception 来实现。

interface CustomThrowable extends Throwable {}
class CustomException extends Exception implements CustomThrowable {}
throw new CustomException();
登入後複製

  PHP 7 中 Error 和 Exception 的继承关系

interface Throwable
    |- Exception implements Throwable
        |- Other Exception classes
    |- Error implements Throwable
        |- TypeError extends Error
        |- ParseError extends Error
        |- AssertionError extends Error
        |- ArithmeticError extends Error
            |- DivizionByZeroError extends ArithmeticError
登入後複製
  • TypeError

  当函数的传参或返回值的数据类型与申明的数据类型不一致时,会抛出 TypeError

function add(int $left, int $right)
{
    return $left + $right;
}
try {
    $value = add('left', 'right');
} catch (TypeError $e) {
    echo $e->getMessage();
}
// 运行以上代码,会输出:
Argument 1 passed to add() must be of the type int, string given
登入後複製

  当开启严格模式时,如果 PHP 内建函数的传参个数与要求的参数不一致,也会抛出 TypeError

declare(strict_types = 1);
try {
    substr('abc');
} catch (TypeError $e) {
    echo $e->getMessage();
}
// 运行以上代码,会输出:
substr() expects at least 2 parameters, 1 given
登入後複製

  默认情况下,PHP 7 处于弱模式。在弱模式下,PHP 7 会尽可能的将传参的数据类型转换为期望的数据类型。例如,如果函数期望的参数类型为 string,而实际传参的数据类型的 int,那么 PHP 会把 int 转换为 string。

// declare(strict_types = 1);
function add(string $left, string $right)
{
    return $left + $right;
}
try {
    $value = add(11, 22);
    echo $value;
} catch (TypeError $e) {
    echo $e->getMessage();
}
// 以上代码运行,会正常输出 33,PHP 会对传参的数据类型做转换(int→string→int)
// 但如将 PHP 改为严格模式,则运行是会抛出 TypeError
Argument 1 passed to add() must be of the type string, int given
登入後複製
  • ParseError

  当在 include 或 require 包含的文件中存在语法错误,或 eval() 函数中的代码中存在语法错误时,会抛出 ParseError

// a.php
$a = 1
$b = 2
// test.php
try {
    require 'a.php';
} catch (ParseError $e) {
    echo $e->getMessage();
}
// 以上代码运行会输出:
syntax error, unexpected '$b' (T_VARIABLE)
// eval 函数中的代码存在语法错误
try {
    eval("$a = 1");
} catch (ParseError $e) {
    echo $e->getMessage();
}
// 以上代码运行会输出:
syntax error, unexpected end of file
登入後複製
  • AssertionError

  当断言失败时,会抛出 AssertionError(此时要求 PHP 配置中 zend.assertions = 1,assert.exception = 1,这两个配置可以在 php.ini 文件中配置,也可以通过 ini_set() 在 PHP 代码中配置)。

ini_set('zend_assertions', 1);
ini_set('assert.exception', 1);
try {
    $test = 1;
    assert($test === 0);
} catch (AssertionError $e) {
    echo $e->getMessage();
}
// 运行以上代码会输出:
assert($test === 0)
登入後複製
  • ArithmeticError

  在 PHP 7 中,目前有两种情况会抛出 ArithmeticError:按位移动操作,第二个参数为负数;使用 intdiv() 函数计算 PHP_INT_MIN 和 -1 的商(如果使用 / 计算 PHP_INT_MIN 和 -1 的商,结果会自动转换为 float 类型)。

try {
    $value = 1 << -1;
} catch (ArithmeticError $e) {
    echo $e->getMessage();
}
// 运行以上代码,会输出:
Bit shift by negative number
try {
    $value = intdiv(PHP_INT_MIN, -1);
} catch (ArithmeticError $e) {
    echo $e->getMessage();
}
// 运行以上代码,会输出:
Division of PHP_INT_MIN by -1 is not an integer
登入後複製
  • DivisionByZeroError

  抛出 DivisionByZeorError 的情况目前也有两种:在进行取模(%)运算时,第二个操作数为 0;使用 intdiv() 计算两个数的商时,除数为 0。如果使用 / 计算两个数的商时除数为 0,PHP 只会产生一个 Warning。并且,如果被除数非 0,则结果为 INF,如果被除数也是 0,则结果为 NaN。

try {
    $value = 1 % 0;
    echo $value;
} catch (DivisionByZeroError $e) {
    echo $e->getMessage(), "\n";
}
// 运行以上代码,会输出:
Modulo by zero
try {
    $value = intdiv(0, 0);
    echo $value;
} catch (DivisionByZeroError $e) {
    echo $e->getMessage(), "\n";
}
// 运行以上代码,会输出:
Division by zero
登入後複製

  通常在实际的业务中,捕获并处理抛出的 Error 并不常见,因为一旦抛出 Error 说明代码存在严重的 BUG,需要修复。所以,在实际的业务中,Error 更多的只是被用来捕获并记录具体的错误日志,然后通知开发者进行 BUG 修复。

以上是聊聊在PHP7對於Error的處理是怎樣的的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:juejin.im
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!