国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > 建站經驗 > 正文

P-HP異常處理淺析

2019-11-02 15:14:16
字體:
來源:轉載
供稿:網友

   這篇文章主要介紹了PHP異常處理淺析,本文著重講解如何捕獲異常,并給出代碼操作實例,需要的朋友可以參考下

  PHP預定了兩個異常類:Exception和ErrorException

   代碼如下:

  Exception {

  /* 屬性 */

  protected string $message ; //異常消息內容

  protected int $code ; //異常代碼號

  protected string $file ; //拋出異常的文件名

  protected int $line ; //拋出異常在該文件中的行號

  /* 方法 */

  public __construct ([ string $message = "" [, int $code = 0 [, Exception $previous = null]]] )

  final public string getMessage ( void ) //異常拋出的信息

  final public Exception getPrevious ( void ) //前一異常

  final public int getCode ( void ) //異常代碼,這是用戶自定義的

  final public string getFile ( void ) //發生異常的文件路勁

  final public int getLine ( void ) //發生異常的行

  final public array getTrace ( void ) //異常追蹤信息(array)

  final public string getTraceAsString ( void ) //異常追蹤信息(string)

  public string __toString ( void ) //試圖直接 將異常對象當作字符串使用時調用子函數的返回值

  final private void __clone ( void ) //克隆異常對象時調用

  }

   代碼如下:

  ErrorException extends Exception {

  /* 屬性 */

  protected int $severity ;

  /* 方法 */

  public __construct ([ string $message = "" [, int $code = 0 [, int $severity = 1 [, string $filename = __FILE__ [, int $lineno = __LINE__ [, Exception $previous = NULL ]]]]]] )

  final public int getSeverity ( void )

  /* 繼承的方法 */

  final public string Exception::getMessage ( void )

  final public Exception Exception::getPrevious ( void )

  final public int Exception::getCode ( void )

  final public string Exception::getFile ( void )

  final public int Exception::getLine ( void )

  final public array Exception::getTrace ( void )

  final public string Exception::getTraceAsString ( void )

  public string Exception::__toString ( void )

  final private void Exception::__clone ( void )

  }

  那么如何捕獲異常?

  (1)PHP可用try...catch...捕獲異常,進行異常處理的代碼必須在try代碼塊內。

   代碼如下:

  try {

  throw new Exception('exception test 1', 1001);

  } catch(Exception $e) {

  echo $e->getMessage().'-'.$e->getCode();

  }

  (2)用戶可以自定義異常處理函數[set_exception_handler],用于沒用用try/catch捕獲的異常。

   代碼如下:

  function exception_handler ( $e ) {

  echo "Uncaught exception: " , $e -> getMessage (), "n" ;

  }

  set_exception_handler ( 'exception_handler' );

  throw new Exception ( 'Uncaught Exception' );

  echo "這行不會執行了";

  可以看到使用ser_exception_handler回調函數處理異常,后續的代碼不會繼續執行,但try-catch可以。

  (3)PHP可用多catch捕獲不同類型異常,并允許在catch代碼塊內再次拋出異常。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 芒康县| 宁远县| 商河县| 青川县| 昌黎县| 乐清市| 浪卡子县| 安阳市| 张家口市| 诸城市| 阳江市| 长治县| 苏尼特右旗| 利津县| 安陆市| 永胜县| 黄浦区| 思南县| 水城县| 乌拉特后旗| 磴口县| 岳阳县| 广昌县| 甘南县| 临城县| 金山区| 炉霍县| 溧阳市| 伊川县| 韶山市| 罗山县| 泸西县| 西华县| 和龙市| 成安县| 招远市| 于都县| 惠水县| 滁州市| 大厂| 牡丹江市|