欧美日韩不卡一区二区三区,www.蜜臀.com,高清国产一区二区三区四区五区,欧美日韩三级视频,欧美性综合,精品国产91久久久久久,99a精品视频在线观看

php語言

PHP7的異常處理詳解

時間:2025-05-10 20:48:40 php語言 我要投稿
  • 相關(guān)推薦

PHP7的異常處理詳解

  在PHP中碰到異常的時候應(yīng)該如何處理呢,就跟隨百分網(wǎng)小編一起去了解下吧,想了解更多相關(guān)信息請持續(xù)關(guān)注我們應(yīng)屆畢業(yè)生考試網(wǎng)!

  前言

  PHP7的改動中,影響比較大的,包括異常處理。

  概述

  更多的異常是直接通過PHP直接處理的,和之前的PHP5不同的是更多的異常是通過Error exceptions來拋出。

  作為一個普通的擴展,Error exceptions會持續(xù)冒出直到匹配到對應(yīng)的catch塊。如果沒有進行匹配,就會觸發(fā)被設(shè)置的set_exception_handler()來執(zhí)行處理,如果沒有默認的異常處理程序,則該異常將被轉(zhuǎn)換為一個致命錯誤,并且將被像一個傳統(tǒng)的錯誤被處理。

  由于Error在錯誤層次結(jié)構(gòu)不繼承異常,像這樣的代碼catch (Exception $e) { ... }在PHP5中并不會捕獲到對應(yīng)的異常。我們可以用代碼catch (Error $e) { ... }或者 set_exception_handler(),來對Error進行處理。

  錯誤的層級結(jié)構(gòu)

  Throwable

  Error 錯誤

  ArithmeticError 算數(shù)錯誤

  PisionByZeroError 除數(shù)為0的錯誤

  AssertionError 聲明錯誤

  ParseError 解析錯誤

  TypeError 類型錯誤

  Exception 異常

  ….

  PHP RFC

  Throwable Interface

  function add(int $left, int $right) {    return $left + $right;}try {    echo add('left', 'right');} catch (Exception $e) {    // Handle exception} catch (Error $e) { // Clearly a different type of object    // Log error and end gracefully    var_dump($e);}

  這里,并沒有出現(xiàn)服務(wù)器500的錯誤。原因在于,PHP7中的Error把它攔截住了,沒有冒泡在服務(wù)器中。

  object(TypeError)#1 (7) {  ["message":protected]=>  string(139) "Argument 1 passed to add() must be of the type integer, string given, called in /Applications/mamp/apache2/htdocs/curl/error.php on line 14"  ["string":"Error":private]=>  string(0) ""  ["code":protected]=>  int(0)  ["file":protected]=>  string(48) "/Applications/mamp/apache2/htdocs/curl/error.php"  ["line":protected]=>  int(9)  ["trace":"Error":private]=>  array(1) {    [0]=>    array(4) {      ["file"]=>      string(48) "/Applications/mamp/apache2/htdocs/curl/error.php"      ["line"]=>      int(14)      ["function"]=>      string(3) "add"      ["args"]=>      array(2) {        [0]=>        string(4) "left"        [1]=>        string(5) "right"      }    }  }  ["previous":"Error":private]=>  NULL}

  這樣我們就可以通過日志的方式記錄他們。

  Exceptions in the engine (for PHP 7)

  function call_method($obj) {    $obj->method();}try {    call_method(null); // oops!} catch (EngineException $e) {    echo "Exception: {$e->getMessage()}/n";}//其實上面的例子我在運行過程中,并沒有被EngineException捕獲異常,經(jīng)過測試,也是通過Error進行的錯誤的攔截

  如果異常沒有被捕獲,PHP將繼續(xù)擔任目前它拋出同樣的致命錯誤。

【PHP7的異常處理詳解】相關(guān)文章:

PHP7系列中的異常處理08-11

PHP7系列之-異常處理06-07

PHP7系列之異常處理08-15

PHP7錯誤處理機制詳解介紹07-20

Java 異常處理09-16

PHP7多線程搭建教程詳解 08-12

PHP異常處理辦法08-08

Java的異常處理及應(yīng)用10-01

Java異常處理語句及解析07-27