標籤:
<?php//語法錯誤//運行時的錯誤//邏輯錯誤//php的錯誤報表層級// display_errors;// ini_set("display_errors", "On");// error_reporting();// error_reporting = E_ALL &~ E_NOTICE; //除了什麼以外error_reporting(0); //屏蔽系統的錯誤資訊function error_handler($errLevel, $errMsg, $errFile, $errLine){ switch($errLevel){ case E_NOTICE; case E_USER_NOTICE; $errType = "notice"; break; case E_WARNING; case E_USER_WARNING; $errType = "warning"; break; case E_ERROR; case E_USER_ERROR; $errType = "error"; break; default: $errType = "UnKnown"; } printf("錯誤的層級為:%s,錯誤的資訊為:%s,發送在%s的%s行", $errLevel, $errMsg, $errFile, $errLine);}set_error_handler("error_handler"); //用這個函數來調用自己定義錯誤函數// echo $err; //如何屏蔽這些系統的錯誤層級資訊echo 3/0;trigger_error("這是一個自訂的錯誤", E_USER_ERROR); //使用者自訂一個錯誤?><?php/* 寫錯誤記錄檔先配置php.ini:error_reporting = E_ALLdisplay_errors = Offlog_errors = Onlog_errors_max_log = 1024error_log = G:/myerror.log //去除前面的分號就是開啟*/error_log("這是一個錯誤資訊"); //用這個函數,就可以向我們配置好的日子檔案中發送錯誤資訊?><?php//try{}catch(){}拋出異常學習//建立可拋出一個異常的函數function checkNum($number){ if($number>1){ throw new Exception("Value must be 1 or below"); } return true;}//在 "try" 代碼塊中觸發異常try{ checkNum(2); //這時候會拋出一個異常,這個異常將會被下面的catch捕獲 //如果沒有異常,返回的就是true,就會執行下面的代碼 //如果異常被拋出,那麼下面一行代碼將不會被輸出,跳到catch()捕獲這個異常 echo ‘If you see this, the number is 1 or below‘;}catch(Exception $e){ //$e 是一個對象 //捕獲異常 echo "<pre>"; print_r($e); echo "</pre>"; // echo $e->getCode(); echo ‘Message: ‘ .$e->getMessage();}?><?php//再看一個執行個體try{ $i = 5; if($i < 10){ throw new exception("$i must greater than 10"); //拋出異常 }}catch(Exception $e){ //捕獲異常 // echo $e->getMessage(); throw new exception(‘errorinfo‘); }?>
php自訂錯誤處理和try{}catch(){}學習