<?php class MyException extends Exception{ public $error = ''; function __construct($error){ parent::__construct($error); $this->error = $error; } function getMsg(){ echo $this->error; } } try{ $file = 'tets.txt'; if(is_file($file)) $a = fopen($file,'r'); else throw new MyException('文本不存在'); }catch(Exception $e){ echo $e->getMessage()."\n"; echo "\n"; echo $e->getMsg()."\n"; } die; try { $error = 'Always throw this error'; echo '執行'; $url = 'http://www.baidu.com'; $a = rand(1,10); echo $a; if($a==4){ echo '正常'; }else if($a>=7){ $error = '鏈接超時或者鏈接出錯,請檢查'; throw new Exception($error); }else{ throw new Exception('xxxx錯誤'); } // Code following an exception is not executed. echo 'Never executed'; } catch (Exception $e) { echo 'Caught exception: ', $e->getMessage(), "\n"; }