php使用try catch 捕捉異常
阿新 • • 發佈:2020-07-20
<?php /**************************************************** * php處理異常 * try中不主動throw,會先出現PHP的系統錯誤 ****************************************************/ header("content-type:test/html:charset=utf-8"); error_reporting(-1); try { $num1 = 3; $num2 = 0; if ($num2 == 0) { throw new Exception("自定義錯誤"); } 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相對於傳統的open($filename, 'r')產生的物件的優點在於不需要開啟檔案控制代碼 不需要關閉控制代碼更加的方便$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("測試異常1"); } catch (Exception $e) { echo $e->getMessage(); // die(); // 終止異常 try { throw new Exception("測試異常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>出現異常,如下:</h2>"; $message .= "<p>" . __CLASS__ . "[{$this->code}:{$this->message}]</p>"; return $message; } /****************自定義異常方法***************/ public function test() { echo "這是自定義錯誤"; } public function stop() { exit("異常 end..."); } } // 開始呼叫 MyException try { echo "出現異常啦"; throw new MyException("測試自定義異常", 3); } catch (MyException $e) { echo $e->getMessage(); } // 巢狀使用 MyException 與 Exception (沒有順序) try { throw new MyException("測試自定義異常"); } 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 "沒有提供檔案"; break; case 1: return "檔案不存在"; break; case 2: return "不是一個檔案"; 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(); } }
try{ echo '捕捉異常'; } catch (throwable $ex) { echo $ex->getMessage(), "<br />\n"; echo $ex->getFile(), "<br />\n"; echo $ex->getLine(), "<br />\n"; echo $ex->getTraceAsString(), "<br />\n"; }