標籤: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異常處理