ini_set('display_errors', 1); $fd = fopen("c:/temp/abc.txt", "r"); echo "継続処理"; a(); echo "継続処理";
$ php index.php
Warning: fopen(c:/temp/abc.txt): failed to open stream: No such file or directory in /home/vagrant/dev/test/index.php on line 5
継続処理
Fatal error: Uncaught Error: Call to undefined function a() in /home/vagrant/dev/test/index.php:8
Stack trace:
#0 {main}
thrown in /home/vagrant/dev/test/index.php on line 8
### set_error_handler
set_error_handler(function($errno, $errstr, $errfile, $errline){ print("エラーが発生しました。$errno, $errstr <br>"); }); $fd = fopen("c:/temp/abc.txt", "r");
$ php index.php
エラーが発生しました。2, fopen(c:/temp/abc.txt): failed to open stream: No such file or directory
### Exception
例外が発生しそうな処理には、try, catch, finallyを使う
set_error_handler(function($errno, $errstr, $errfile, $errline) { if (!(error_reporting() & $errno)) { return; } throw new ErrorException($errstr, $errno, 0, $errfile, $errline); }); try { $fd = fopen("c:/temp/abc.txt", "r"); } catch(Exception $e){ echo "例外が発生しました。".$e->getMessage(); }