回答編集履歴

4

引用範囲修正

2016/11/24 07:33

投稿

Y.H.
Y.H.

スコア7914

test CHANGED
@@ -66,9 +66,9 @@
66
66
 
67
67
  > 投げたCodeで処理を分けるのって、一般的に見てアリですか?
68
68
 
69
- 局所的なものなら問題ないんじゃないですかね?
70
69
 
71
70
 
71
+ 局所的なものなら問題ないんじゃないですかね?
72
72
 
73
73
  以下のように自分でExceptionを作成することもできます。(実行確認してませんのであくまで参考です。)
74
74
 

3

セパレーター追加

2016/11/24 07:33

投稿

Y.H.
Y.H.

スコア7914

test CHANGED
@@ -56,7 +56,7 @@
56
56
 
57
57
  ```
58
58
 
59
-
59
+ ---
60
60
 
61
61
  > それとすみません、関連した質問として
62
62
 
@@ -70,7 +70,7 @@
70
70
 
71
71
 
72
72
 
73
- 以下のように自分でExceptionを作成することもできます。
73
+ 以下のように自分でExceptionを作成することもできます。(実行確認してませんのであくまで参考です。)
74
74
 
75
75
 
76
76
 

2

追記

2016/11/24 07:32

投稿

Y.H.
Y.H.

スコア7914

test CHANGED
@@ -34,10 +34,6 @@
34
34
 
35
35
 
36
36
 
37
-
38
-
39
-
40
-
41
37
  以下のように書くとすべてのExceptionをcatchします。
42
38
 
43
39
 
@@ -59,3 +55,71 @@
59
55
  }
60
56
 
61
57
  ```
58
+
59
+
60
+
61
+ > それとすみません、関連した質問として
62
+
63
+ > VBAのGotoステートメントのように例外を分けたいのですが
64
+
65
+ > ・・・略・・・
66
+
67
+ > 投げたCodeで処理を分けるのって、一般的に見てアリですか?
68
+
69
+ 局所的なものなら問題ないんじゃないですかね?
70
+
71
+
72
+
73
+ 以下のように自分でExceptionを作成することもできます。
74
+
75
+
76
+
77
+ ```PHP
78
+
79
+ class SystemException extends Exception {
80
+
81
+ public function __construct(){
82
+
83
+ parent::__construct('500 Internal Server Error');
84
+
85
+ }
86
+
87
+
88
+
89
+ class NotFoundException extends Exception {
90
+
91
+ public function __construct(){
92
+
93
+ parent::__construct('404 Not Found');
94
+
95
+ }
96
+
97
+
98
+
99
+ try {
100
+
101
+ if ($err1) throw new NotFondException("");
102
+
103
+ if ($err2) throw new NotFondException("");
104
+
105
+ if ($err3) throw new SysFondException("");
106
+
107
+ } catch (NotFoundException $nfex) {
108
+
109
+ header("HTTP/1.0 " . $nfex->getMessage());
110
+
111
+ exit();
112
+
113
+ } catch (SystemException $sysex) {
114
+
115
+ header("HTTP/1.0 " . $sysex->getMessage());
116
+
117
+ // エラーログ出力など
118
+
119
+ exit();
120
+
121
+ }
122
+
123
+ ```
124
+
125
+

1

コードないコメント追加

2016/11/24 07:29

投稿

Y.H.
Y.H.

スコア7914

test CHANGED
@@ -16,9 +16,17 @@
16
16
 
17
17
  // PDOExceptionをCatchしたときの例外処理
18
18
 
19
+ // DB接続ができなかったのでDBのCloseなどは不要
20
+
19
21
  } catch (Exception $ex) {
20
22
 
21
23
  // ExceptionをCatchしたときの例外処理
24
+
25
+ // 「なんかの処理」でファイルオープンに失敗していた。
26
+
27
+ // PDOExceptionじゃ無かったのでDBへの接続はできている。
28
+
29
+ // DBのClose処理が必要
22
30
 
23
31
  }
24
32