回答編集履歴
1
エラーの制御を追記
test
CHANGED
@@ -35,3 +35,103 @@
|
|
35
35
|
```
|
36
36
|
|
37
37
|
多くの上級言語で採用されている書き方です
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
# 追記
|
42
|
+
|
43
|
+
エラーは自分用に制御できます
|
44
|
+
|
45
|
+
```PHP
|
46
|
+
|
47
|
+
<?PHP
|
48
|
+
|
49
|
+
set_error_handler('myErrorHandler');
|
50
|
+
|
51
|
+
function myErrorHandler($errno, $errstr, $errfile, $errline)
|
52
|
+
|
53
|
+
{
|
54
|
+
|
55
|
+
switch($errno) {
|
56
|
+
|
57
|
+
case E_NOTICE:
|
58
|
+
|
59
|
+
throw new exception("common notice:".$errstr."<br>");
|
60
|
+
|
61
|
+
break;
|
62
|
+
|
63
|
+
case E_USER_NOTICE:
|
64
|
+
|
65
|
+
throw new exception("my notice:".$errstr."<br>");
|
66
|
+
|
67
|
+
break;
|
68
|
+
|
69
|
+
default:
|
70
|
+
|
71
|
+
return false;
|
72
|
+
|
73
|
+
}
|
74
|
+
|
75
|
+
return true;
|
76
|
+
|
77
|
+
}
|
78
|
+
|
79
|
+
//(1) 予めerrorを想定して自分用のNOTICEを作成
|
80
|
+
|
81
|
+
try{
|
82
|
+
|
83
|
+
if(!isset($a)) trigger_error("no a!",E_USER_NOTICE);
|
84
|
+
|
85
|
+
print $a;
|
86
|
+
|
87
|
+
print 1/0;
|
88
|
+
|
89
|
+
}catch(exception $e){
|
90
|
+
|
91
|
+
print $e->getMessage();
|
92
|
+
|
93
|
+
}
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
//(2) 発生したNOTICEを自分用のエラーメッセージを表示
|
98
|
+
|
99
|
+
try{
|
100
|
+
|
101
|
+
print $a;
|
102
|
+
|
103
|
+
print 1/0;
|
104
|
+
|
105
|
+
}catch(exception $e){
|
106
|
+
|
107
|
+
print $e->getMessage();
|
108
|
+
|
109
|
+
}
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
//(3) NOTICEではないエラーはシステムに任せる
|
114
|
+
|
115
|
+
try{
|
116
|
+
|
117
|
+
print 1/0;
|
118
|
+
|
119
|
+
}catch(exception $e){
|
120
|
+
|
121
|
+
print $e->getMessage();
|
122
|
+
|
123
|
+
}
|
124
|
+
|
125
|
+
//エラーにかかわらず後ろには流れている
|
126
|
+
|
127
|
+
print "continue...";
|
128
|
+
|
129
|
+
?>
|
130
|
+
|
131
|
+
```
|
132
|
+
|
133
|
+
PDOなどでよく使われるのは、Exceptionを前提に例外処理が用意されているからです
|
134
|
+
|
135
|
+
ご自身でもなにかライブラリを作るときにはこのように例外を用意しておくと
|
136
|
+
|
137
|
+
プログラムを止めずにバグを発見できます。
|