基于PHP7錯(cuò)誤處理與異常處理方法(詳解)
PHP7錯(cuò)誤處理
PHP 7 改變了大多數(shù)錯(cuò)誤的報(bào)告方式。不同于傳統(tǒng)(PHP 5)的錯(cuò)誤報(bào)告機(jī)制,現(xiàn)在大多數(shù)錯(cuò)誤被作為 Error 異常拋出。
這種 Error 異常可以像 Exception 異常一樣被第一個(gè)匹配的 try / catch 塊所捕獲。如果沒有匹配的 catch 塊,則調(diào)用異常處理函數(shù)(事先通過 set_exception_handler() 注冊(cè))進(jìn)行處理。 如果尚未注冊(cè)異常處理函數(shù),則按照傳統(tǒng)方式處理:被報(bào)告為一個(gè)致命錯(cuò)誤(Fatal Error)。
Error 類并非繼承自 Exception 類,所以不能用 catch (Exception e)...來捕獲Error。你可以用catch(Errore) { … },或者通過注冊(cè)異常處理函數(shù)( set_exception_handler())來捕獲 Error。
Error 層次結(jié)構(gòu)
Throwable Error ArithmeticError DivisionByZeroError AssertionError ParseError TypeError Exception ...
try { // Code that may throw an Exception or Error. } catch (Throwable $t) { // Executed only in PHP 7, will not match in PHP 5 } catch (Exception $e) { // Executed only in PHP 5, will not be reached in PHP 7 } up down 9 lubaev dot ka at gmail dot com ¶ 11 months ago php 7.1 try { // Code that may throw an Exception or ArithmeticError. } catch (ArithmeticError | Exception $e) { // pass }
擴(kuò)展(extend) PHP內(nèi)置的異常處理類
用戶可以用自定義的異常處理類來擴(kuò)展PHP內(nèi)置的異常處理類。以下的代碼說明了在內(nèi)置的異常處理類中,哪些屬性和方法在子類中是可訪問和繼承的。
Example #1 內(nèi)置的異常處理類
<?php class Exception { protected $message = 'Unknown exception'; // 異常信息 private $string; // __toString cache protected $code = 0; // 用戶自定義異常代碼 protected $file; // 發(fā)生異常的文件名 protected $line; // 發(fā)生異常的代碼行號(hào) private $trace; // backtrace private $previous; // previous exception if nested exception public function __construct($message = null, $code = 0, Exception $previous = null); final private function __clone(); // Inhibits cloning of exceptions. final public function getMessage(); // 返回異常信息 final public function getCode(); // 返回異常代碼 final public function getFile(); // 返回發(fā)生異常的文件名 final public function getLine(); // 返回發(fā)生異常的代碼行號(hào) final public function getTrace(); // backtrace() 數(shù)組 final public function getPrevious(); // 之前的 exception final public function getTraceAsString(); // 已格成化成字符串的 getTrace() 信息 // Overrideable public function __toString(); // 可輸出的字符串 } ?> 如果使用自定義的類來擴(kuò)展內(nèi)置異常處理類,并且要重新定義構(gòu)造函數(shù)的話,建議同時(shí)調(diào)用 parent::__construct() 來檢查所有的變量是否已被賦值。當(dāng)對(duì)象要輸出字符串的時(shí)候,可以重載 __toString() 并自定義輸出的樣式。 Note: Exception 對(duì)象不能被復(fù)制。嘗試對(duì) Exception 對(duì)象復(fù)制 會(huì)導(dǎo)致一個(gè) E_ERROR 級(jí)別的錯(cuò)誤。
<?php /** * 自定義一個(gè)異常處理類 */ class MyException extends Exception { // 重定義構(gòu)造器使 message 變?yōu)楸仨毐恢付ǖ膶傩? public function __construct($message, $code = 0, Exception $previous = null) { // 自定義的代碼 // 確保所有變量都被正確賦值 parent::__construct($message, $code, $previous); } // 自定義字符串輸出的樣式 public function __toString() { return __CLASS__ . ": [{$this->code}]: {$this->message}\n"; } public function customFunction() { echo "A custom function for this type of exception\n"; } } /** * 創(chuàng)建一個(gè)用于測(cè)試異常處理機(jī)制的類 */ class TestException { public $var; const THROW_NONE = 0; const THROW_CUSTOM = 1; const THROW_DEFAULT = 2; function __construct($avalue = self::THROW_NONE) { switch ($avalue) { case self::THROW_CUSTOM: // 拋出自定義異常 throw new MyException('1 is an invalid parameter', 5); break; case self::THROW_DEFAULT: // 拋出默認(rèn)的異常 throw new Exception('2 is not allowed as a parameter', 6); break; default: // 沒有異常的情況下,創(chuàng)建一個(gè)對(duì)象 $this->var = $avalue; break; } } }
以上這篇基于PHP7錯(cuò)誤處理與異常處理方法(詳解)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
PHP的HTTP客戶端Guzzle簡(jiǎn)單使用方法分析
這篇文章主要介紹了PHP的HTTP客戶端Guzzle簡(jiǎn)單使用方法,結(jié)合實(shí)例形式分析了Guzzle的功能、請(qǐng)求、cookie操作等相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-10-10PHP定時(shí)執(zhí)行任務(wù)的3種方法詳解
PHP不支持多線程,有時(shí)候處理問題不是那么方便,今天談?wù)撘幌翽HP定時(shí)執(zhí)行的方法,感興趣的小伙伴們可以參考一下2015-12-12PHP聚合式迭代器接口IteratorAggregate用法分析
這篇文章主要介紹了PHP聚合式迭代器接口IteratorAggregate用法,結(jié)合實(shí)例形式分析了聚合式迭代器接口IteratorAggregate的概念、功能、定義及使用方法,需要的朋友可以參考下2017-12-12PHP獲取redis里不存在的6位隨機(jī)數(shù)應(yīng)用示例【設(shè)置24小時(shí)過時(shí)】
這篇文章主要介紹了PHP獲取redis里不存在的6位隨機(jī)數(shù)的方法,可設(shè)置24小時(shí)過時(shí)限制,涉及php字符串及數(shù)據(jù)庫相關(guān)操作技巧,需要的朋友可以參考下2017-06-06php實(shí)現(xiàn)的驗(yàn)證碼文件類實(shí)例
這篇文章主要介紹了php實(shí)現(xiàn)的驗(yàn)證碼文件類,實(shí)例分析了php生成驗(yàn)證碼文件的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-06-06PHP實(shí)現(xiàn)下載斷點(diǎn)續(xù)傳的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)下載斷點(diǎn)續(xù)傳的方法,通過自定義函數(shù)來實(shí)現(xiàn)PHP的斷點(diǎn)續(xù)傳下載方法,涉及文件的常見操作與指針和緩沖的用法,代碼中備有較為詳盡的注釋便于閱讀和理解,需要的朋友可以參考下2014-11-11