亚洲最大看欧美片,亚洲图揄拍自拍另类图片,欧美精品v国产精品v呦,日本在线精品视频免费

  • 站長(zhǎng)資訊網(wǎng)
    最全最豐富的資訊網(wǎng)站

    PHP出現(xiàn)異常該怎么辦

    本篇文章給大家介紹一下解決PHP出現(xiàn)異常的方法。有一定的參考價(jià)值,有需要的朋友可以參考一下,希望對(duì)大家有所幫助。

    PHP出現(xiàn)異常該怎么辦

    PHP出現(xiàn)異常該怎么辦

    <?php /****************************************************  * php處理異常  * try中不主動(dòng)throw,會(huì)先出現(xiàn)PHP的系統(tǒng)錯(cuò)誤  ****************************************************/ header("content-type:test/html:charset=utf-8"); error_reporting(-1); try {     $num1 = 3;     $num2 = 0;     if ($num2 == 0) {         throw new Exception("自定義錯(cuò)誤");     } else {         $res = $num1 / $num2;     }  } catch (Exception $e) {     echo $e->getMessage();     // die(); // 終止異常 } /******************************************************  * php+mysql+pdo  *****************************************************/ try {     $pdo = new PDO("mysql:host=localhost;dbname=mysql", "root", ""); } catch (PDOException $e) {     echo $e->getMessage();     // die(); // 終止異常 } /******************************************************  * php+文件異常  *****************************************************/  /**  * PHP 讀取大文件 SplFileObject :  * https://blog.csdn.net/ekliu/article/details/8855907  */ // SqlFileObject相對(duì)于傳統(tǒng)的open($filename, 'r')產(chǎn)生的對(duì)象的優(yōu)點(diǎn)在于不需要打開文件句柄  不需要關(guān)閉句柄更加的方便 $handle = new SplFileObject("sid_list.txt"); while (!$handle->eof()) {     $item = $handle->fgets(); }  try {     $pdo = new SplFileObject("text.txt", "r");     echo "read File"; } catch (Exception $e) {     echo $e->getMessage();     // die(); // 終止異常 } /******************************************************  * php異常 嵌套  *****************************************************/ try {     throw new Exception("測(cè)試異常1"); } catch (Exception $e) {     echo $e->getMessage();     // die(); // 終止異常     try {         throw new Exception("測(cè)試異常2");     } catch (Exception $e) {         echo $e->getMessage();     } }  /******************************************************  * php異常 自定義異常封裝  *****************************************************/ class MyException extends Exception {     public function __construct($message = "", $code = 0, $previous = null)     {         parent::__construct($message, $code, $previous);     }      public function __toString()     {         $message = "<h2>出現(xiàn)異常,如下:</h2>";         $message .= "<p>" . __CLASS__ . "[{$this->code}:{$this->message}]</p>";         return $message;     }      /****************自定義異常方法***************/     public function test()     {         echo "這是自定義錯(cuò)誤";     }      public function stop()     {         exit("異常 end...");     } }  // 開始調(diào)用 MyException try {     echo "出現(xiàn)異常啦";     throw new MyException("測(cè)試自定義異常", 3); } catch (MyException $e) {     echo $e->getMessage(); }  // 嵌套使用 MyException 與 Exception (沒(méi)有順序) try {     throw new MyException("測(cè)試自定義異常"); } catch (Exception $e) {     echo $e->getMessage(); } catch (MyException $e) {     echo $e->getMessage(); }  /******************************************************  * php異常 自定義異常封裝    文件  *****************************************************/ class FileException extends Exception {     public function getDetails()     {         switch ($this->code) {             case 0:                 return "沒(méi)有提供文件";                 break;             case 1:                 return "文件不存在";                 break;             case 2:                 return "不是一個(gè)文件";                 break;             case 3:                 return "文件不可寫";                 break;             case 4:                 return "非法文件的操作模式";                 break;         }     } }  class WriteData {     private $_message = "";     private $_fp = null;      public function __construct($filename = null, $mode = "w")     {         $this->_message = "文件:{$filename} ; 模式:{$mode}";         if (empty($filename)) throw new FileException($this->_message, 0);         if (!file_exists($filename)) throw new FileException($this->_message, 1);         if (!is_file($filename)) throw new FileException($this->_message, 2);         // is_writable — 判斷給定的文件名是否可寫         if (!is_writable($filename)) throw new FileException($this->_message, 3);         if (!in_array($mode, array("w", "w+", "a", "a         +"))) throw new FileException($this->_message, 4);          $this->_fp = fopen($filename, $mode);     }      public function write($data)     {         if (@!fwrite($this->_fp, $data . PHP_EOL)) throw new FileException($this->_message, 5);     }      public function close()     {         if ($this->_fp) {             if (!fclose($this->_fp)) throw new FileException($this->_message, 6);             $this->_fp = null;          }     }      public function __destruct()     {         $this->close();     } }

    推薦學(xué)習(xí):php視頻教程

    贊(0)
    分享到: 更多 (0)
    網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)