| <?php// exception クラスを継承て 独自の例外クラスを定義
 class FileNotFoundException extends exception {
 }
 
 class TestClass {
 function read($filename){
 if (!file_exists($filename)) {
 // ファイルが無い場合は、例外「FileNotFoundException」を発生させる
 throw new FileNotFoundException("ファイル:" . $filename . "が存在しません!!");
 }
 }
 }
 
 try{
 $class = new TestClass();
 $class->read("test_file.txt");
 } catch(FileNotFoundException $e) {
 echo "例外キャッチ:", $e->getMessage(), "\n";
 }
 ?>
 
 ●実行結果
 例外キャッチ:ファイル:test_file.txtが存在しません!!
 
 
 
 |