PHPエラー処理例外

WBOY
リリース: 2016-06-20 12:25:31
オリジナル
927 人が閲覧しました

通常、カプセル化されたクラスが完全で独立していることを望み、内部コードの実行に外部から介入する必要がないため、プログラマにテスト用の追加コードの作成を依頼します。クラス内のメソッドにエラーがないかどうか。これは非常に不合理です。

クラスや外部コードを呼び出すプログラマーに依存するのではなく、クラス内部のエラー処理の責任に集中する必要があります。通常、クラスを使用するプログラマーは内部エラーの処理方法を知らないからです。メソッドによって発生したエラー。

例外クラス

一般的な記述:

< ?phpclass Server{    function  __construct($file) {        $this->file = $file;        if(!file_exists($file)){            throw new Exception("file '$file' does not exist.");        }    }}class Client{    static function init() {        try{            $conf = new Server(dirname(__FILE__)."/conf01.xml"); //测试用例            $conf->write();        }catch(Exception $e){            print $e;   //这里可以打印错误或者做其他事情        }    }}Client::init();?>
ログイン後にコピー

例外がスローされると、呼び出しスコープ China の catch 句が呼び出され、自動的に例外が発生します。例外がスローされるとクラスメソッドの実行が停止するため、オブジェクトをパラメータとして渡しているため、try 句から catch 句に制御が移ります

しかし、問題がありますつまり、複数の判定を書いて投げても結局一つしかキャッチできないので、エラーが発生する可能性があるので、何らかの処理を行う必要があります

<🎜。 > 高度な書き込み

< ?phpclass Server {    function  __construct($file) {        $this->file = $file;        if(!file_exists($file)){            throw new FileException("file '$this->file' does not exist.");        }    }    function write() {        if(! is_writable($this->file)){            throw new BadException("file '$this->file' is bad.");  //测试我将文件改成不能写的权限 chmod 000 conf01.xml        }    }}class BadException extends Exception{ //需要创建一个新的Exception类,名字是可以diy的,不过需要继承Exception类}class Client{    static function init() {        try{            $conf = new Server(dirname(__FILE__)."/conf01.xml");            $conf->write();        }catch(FileException $e){            print $e;        }catch(BadException $e){  //这里只执行了一个Exception,不过也是我们的目标的BadException            print $e;        }catch(Exception $e){  //这里是为了做后备,为了获取一些其他不能确认的Exception            print $e;        }    }}Client::init();?>
ログイン後にコピー
結果出力 :

exception 'BadException' with message 'file 'Downloads/conf01.xml' is bad.' in Downloads/Untitled.php:15Stack trace:#0 Downloads/Untitled.php(28): Server->write()#1 /Downloads/Untitled.php(39): Client::init()#2 {main}
ログイン後にコピー

なお、前のものが一致すると、次のものが順番に一致します。トリガーされません。

例外クラスを継承し、独自の出力をカスタマイズします

< ?phpclass Server {    function  __construct($file) {        $this->file = $file;        if(!file_exists($file)){            throw new FileException("file '$this->file' does not exist.");        }    }    function write() {        if(! is_writable($this->file)){            throw new BadException("file '$this->file' is not writeable.");        }        if(! is_readable($this->file)){            throw new SpecilException($this->file);        }    }}class BadException extends Exception{}class SpecilException extends Exception{    function __construct($file){  //这里可以创建一些自己的Exception规则,而不单纯是支持打印错误        echo "this is a Specil error,'$file' is not readable";    }}class Client{    static function init() {        try{            $conf = new Server(dirname(__FILE__)."/conf01.xml");            $conf->write();        }catch(FileException $e){            print $e;        }catch(BadException $e){            print $e;        }catch(SpecilException $e){  //我将测试文件改成不能读的状态来测试            print $e;        }catch(Exception $e){            print $e;        }    }}Client::init();?>
ログイン後にコピー
出力結果:

this is a Specil error,'/Downloads/conf01.xml' is not readableexception 'SpecilException' in/Downloads/Untitled.php:19Stack trace:#0 /Downloads/Untitled.php(40): Server->write()#1 /Downloads/Untitled.php(53): Client::init()#2 {main}
ログイン後にコピー
関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!