php異常處理

來源:互聯網
上載者:User

標籤:des   style   blog   io   color   os   ar   sp   strong   

異常處理的過程:

判斷出錯,然後throw new Exception(‘自己填寫輸出的message‘,錯誤編號code)→

通過

1 try{2      //將要測試的代碼        3 }catch(Exception $e){           //$e為throw過來的對象$error4       $e->getMessage();//new Exception對象的message5       $e->getCode();//new Exception對象的code6     7 }

Exception是系統的異常處理類

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 )final public string getTraceAsString ( void )public string __toString ( void )final private void __clone ( void )

}

通常用的方法是——getMessage()、getLine()、getCode()、getFile().

 

對異常通常用一個日誌類errlog記錄起來

1 class errlog{2       public static function logs($err){3             $fp=fopen(‘檔案路徑‘,‘開啟檔案‘);//開啟檔案準備裝東西4             $err=date(‘y-m-d   h-i=s‘)."\r\n".$err;//把時間也記錄下來,txt檔案的換行用\r\n5             fwrite(‘寫進的路徑fp‘,‘$err‘)6     } 7 8 }

在catch下errlog::logs($err),調用類,把內容寫進去

php異常處理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.