Exceptionクラス

制御構造例外処理

コンストラクタ

 状態:-  閲覧数:2,694  投稿日:2014-02-27  更新日:2014-03-03  
Exception::__construct
例外作成
・コンストラクタにつき、new Exceptionで、Exceptionクラスのインスタンスオブジェクトを生成
([ string $message = "" [, int $code = 0 [, Exception $previous = NULL ]]] )
Exception::__construct( スローする例外メッセージ [, 例外コード [,以前に使われた例外]])
・第 1 引数 … スローする例外メッセージ
・第 2 引数 … オプション/例外コード
・第 3 引数 … オプション/以前に使われた例外。例外の連結に使用


コード例

 閲覧数:998 投稿日:2014-02-28 更新日:2014-03-01 
例外を投げているだけで補足していないため、エラーとなる例
・明示的に例外をスローしているが、例外処理(catchブロック、より正確にはtry~catch)を記述していないため、Fatal errorとなる例。例外処理を行うには、例外を捕捉する必要がある
throw new Exception('例外が発生しました');
/*
出力:Fatal error: Uncaught exception 'Exception' with message '例外が発生しました'
*/



Exceptionクラスのメソッド


エラーは発生していないが、throwで例外を強制的に投げている例
・tryブロック内の処理で例外を強制的に発生させ、Exceptionクラスのオブジェクトを投げる
・続いて、「例外クラス名でExceptionを指定したcatchブロック」で例外を捕捉
try {
$error = "例外を発生させます。";
throw new Exception($error);
print "直前で例外が発生しているためここは表示されませ ん。";
} catch (Exception $e) {
print "例外発生!<hr>";
print "例外メッセージ:" . $e->getMessage();
print "<br>";
print "例外コード:" . $e->getCode();
print "<br>";
//print "ファイル名:" . $e->getFile();
//print "<br>";
print "行番号:" . $e->getLine();
print "<pre>";
print "バックトレース:";
print "<br>";
print_r( $e->getTrace( ) );
print "</pre>";
}

print "<hr>例外処理後、処理を続けます。";

・結果
例外発生!
例外メッセージ:例外を発生させます。
例外コード:0
行番号:19
バックトレース:
Array
(
)
例外処理後、処理を続けます。



例外ハンドラ

set_error_handler

コメント投稿(ログインが必要)



類似度ページランキング
順位 ページタイトル抜粋
1 Exceptionクラス 100
2 ErrorExceptionクラス 83
3 Doctrine 60
4 include_path 50
5 popen 47
6 setcookie関数 43
7 unexpected T_VARIABLE 42
8 include 42
9 phpinfo 42
10 syntax error 42
11 session.gc_maxlifetime 41
12 クラス 40
13 define と const の違い 40
14 session_save_path() 39
15 unexpected T_STRING 38
16 session_start() 37
17 session.hash_function 36
18 Generators 36
19 preg_match 36
20 isset 35
2026/7/25 5:22 更新
週間人気ページランキング / 7-18 → 7-24
順位 ページタイトル抜粋 アクセス数
1 PHP用語 7
2 マジックメソッド(クラスとオブジェクト) カテゴリー 4
3 遭遇例1.存在する間違ったデータベース名を指定 3
3 リファレンス基本操作は3通り 3
3 ログイン 3
4 プログラムの基本三構造 | 制御構造 2
4 You currently have Essential access which includes access to Twitter API v2 endpoints only. If you need access to this endpoint, you’ll need to apply for Elevated access via the Developer Portal. | Twitter API(エラーメッセージ) 2
4 htmlspecialchars / htmlentities / addslashes / mysql_real_escape_string / mysqli_real_escape_string | セキュリティ 2
4 デフォルト引数 | 関数 2
4 Fatal error: Uncaught Error: Class 'Mecab_Tagger' not found in /var/www/★★.php:☆☆ | Fatal error(エラーメッセージ) 2
4 CodeIgniter 2
5 コンストラクタ デストラクタ | クラスとオブジェクト 1
5 PHPでcurlを使用するためには?/ 2.オプション設定 / curlでPOST送信 1
5 「添え字(インデックス)」と「要素(値)」で、構成されている 1
5 サニタイズ | セキュリティ 1
5 演算子 1
5 Strict Standards: DateTime::__construct() [function.DateTime---construct]: It is | エラーメッセージ 1
5 型の相互変換 | 型 1
5 Doctrine | データベース関連 1
5 トレイト | クラスとオブジェクト 1
2026/7/25 5:05 更新