php自訂錯誤處理和try{}catch(){}學習

來源:互聯網
上載者:User

標籤:

<?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(){}學習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.