-
Notifications
You must be signed in to change notification settings - Fork 269
/
Copy pathexception_class.phpt
360 lines (335 loc) · 8.32 KB
/
exception_class.phpt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
--TEST--
PHP Spec test generated from ./exception_handling/exception_class.php
--FILE--
<?php
/*
+-------------------------------------------------------------+
| Copyright (c) 2014 Facebook, Inc. (http://www.facebook.com) |
+-------------------------------------------------------------+
*/
error_reporting(-1);
// ---------------------------------------------------------------
function displayExceptionObject(Exception $e)
{
echo "\$e = >$e<\n"; // calls __toString
echo "getMessage: >".$e->getMessage()."<\n";
echo "getCode: >".$e->getCode()."<\n";
echo "getPrevious: >".$e->getPrevious()."<\n";
echo "getFile: >".$e->getFile()."<\n";
echo "getLine: >".$e->getLine()."<\n";
echo "getTraceAsString: >".$e->getTraceAsString()."<\n";
$traceInfo = $e->getTrace();
var_dump($traceInfo);
echo "Trace Info:".((count($traceInfo) == 0) ? " none\n" : "\n");
foreach ($traceInfo as $traceInfoKey => $traceLevel) // process all traceback levels
{
echo "Key[$traceInfoKey]:\n";
foreach ($traceLevel as $levelKey => $levelVal) // process one traceback level
{
if ($levelKey != "args")
{
echo " Key[$levelKey] => >$levelVal<\n";
}
else
{
echo " Key[$levelKey]:\n";
foreach ($levelVal as $argKey => $argVal) // process all args for that level
{
echo " Key[$argKey] => >$argVal<\n";
}
}
}
}
}
// ---------------------------------------------------------------
function fL1($p1 = -10)
{
try {
echo "fL1: In try-block\n";
// throw new Exception();
throw new Exception("fL1 Message", 123);
}
catch (Exception $e) {
echo "fL1: In catch-block\n";
displayExceptionObject($e);
}
finally {
echo "fL1: In finally-block\n";
}
echo "fL1: Beyond try/catch/finally blocks\n==========\n";
echo "fL1: Calling fL2\n";
$a = -4.5;
fL2(2.3, $a); // pass 2nd arg as a non-literal to see how traceback handles it
//fL2(2.3); // see what happens when a default argument value is used
}
// ---------------------------------------------------------------
function fL2($p1, $p2 = -100)
{
try {
echo "fL2: In try-block\n";
// throw new Exception();
throw new Exception("fL2 Message", 234);
}
catch (Exception $e) {
echo "fL2: In catch-block\n";
displayExceptionObject($e);
}
finally {
echo "fL2: In finally-block\n";
}
echo "fL2: Beyond try/catch/finally blocks\n==========\n";
echo "fL2: Calling fL3\n";
$a = "xyz"; $b = NULL; $c = TRUE;
fL3($a, $b, $c); // pass args as a non-literal to see how traceback handles them
//fL3($a, $b); // see what happens when a default argument value is used
}
// ---------------------------------------------------------------
function fL3($p1, $p2, $p3 = -1000)
{
try {
echo "fL3: In try-block\n";
// throw new Exception();
throw new Exception("fL3 Message", 345);
}
catch (Exception $e) {
echo "fL3: In catch-block\n";
displayExceptionObject($e);
}
finally {
echo "fL3: In finally-block\n";
}
echo "fL3: Beyond try/catch/finally blocks\n==========\n";
}
// ---------------------------------------------------------------
try {
echo "L0: In try-block\n";
// throw new Exception();
throw new Exception("L0 Message", -1);
}
catch (Exception $e) {
echo "L0: In catch-block\n";
displayExceptionObject($e);
}
finally {
echo "L0: In finally-block\n";
}
echo "L0: Beyond try/catch/finally blocks\n==========\n";
echo "L0: Calling fL1\n";
fL1(10);
//fL1(); // see what happens when a default argument value is used
--EXPECTF--
L0: In try-block
L0: In catch-block
$e = >Exception: L0 Message in %s%eexception_handling%eexception_class.php:127
Stack trace:
#0 {main}<
getMessage: >L0 Message<
getCode: >-1<
getPrevious: ><
getFile: >%s%eexception_handling%eexception_class.php<
getLine: >127<
getTraceAsString: >#0 {main}<
array(0) {
}
Trace Info: none
L0: In finally-block
L0: Beyond try/catch/finally blocks
==========
L0: Calling fL1
fL1: In try-block
fL1: In catch-block
$e = >Exception: fL1 Message in %s%eexception_handling%eexception_class.php:55
Stack trace:
#0 %s%eexception_handling%eexception_class.php(140): fL1(10)
#1 {main}<
getMessage: >fL1 Message<
getCode: >123<
getPrevious: ><
getFile: >%s%eexception_handling%eexception_class.php<
getLine: >55<
getTraceAsString: >#0 %s%eexception_handling%eexception_class.php(140): fL1(10)
#1 {main}<
array(1) {
[0]=>
array(4) {
["file"]=>
string(%d) "%s%eexception_handling%eexception_class.php"
["line"]=>
int(140)
["function"]=>
string(3) "fL1"
["args"]=>
array(1) {
[0]=>
%Sint(10)
}
}
}
Trace Info:
Key[0]:
Key[file] => >%s%eexception_handling%eexception_class.php<
Key[line] => >140<
Key[function] => >fL1<
Key[args]:
Key[0] => >10<
fL1: In finally-block
fL1: Beyond try/catch/finally blocks
==========
fL1: Calling fL2
fL2: In try-block
fL2: In catch-block
$e = >Exception: fL2 Message in %s%eexception_handling%eexception_class.php:81
Stack trace:
#0 %s%eexception_handling%eexception_class.php(69): fL2(2.3, -4.5)
#1 %s%eexception_handling%eexception_class.php(140): fL1(10)
#2 {main}<
getMessage: >fL2 Message<
getCode: >234<
getPrevious: ><
getFile: >%s%eexception_handling%eexception_class.php<
getLine: >81<
getTraceAsString: >#0 %s%eexception_handling%eexception_class.php(69): fL2(2.3, -4.5)
#1 %s%eexception_handling%eexception_class.php(140): fL1(10)
#2 {main}<
array(2) {
[0]=>
array(4) {
["file"]=>
string(%d) "%s%eexception_handling%eexception_class.php"
["line"]=>
int(69)
["function"]=>
string(3) "fL2"
["args"]=>
array(2) {
[0]=>
%Sfloat(2.3)
[1]=>
%Sfloat(-4.5)
}
}
[1]=>
array(4) {
["file"]=>
string(%d) "%s%eexception_handling%eexception_class.php"
["line"]=>
int(140)
["function"]=>
string(3) "fL1"
["args"]=>
array(1) {
[0]=>
%Sint(10)
}
}
}
Trace Info:
Key[0]:
Key[file] => >%s%eexception_handling%eexception_class.php<
Key[line] => >69<
Key[function] => >fL2<
Key[args]:
Key[0] => >2.3<
Key[1] => >-4.5<
Key[1]:
Key[file] => >%s%eexception_handling%eexception_class.php<
Key[line] => >140<
Key[function] => >fL1<
Key[args]:
Key[0] => >10<
fL2: In finally-block
fL2: Beyond try/catch/finally blocks
==========
fL2: Calling fL3
fL3: In try-block
fL3: In catch-block
$e = >Exception: fL3 Message in %s%eexception_handling%eexception_class.php:107
Stack trace:
#0 %s%eexception_handling%eexception_class.php(95): fL3('xyz', NULL, true)
#1 %s%eexception_handling%eexception_class.php(69): fL2(2.3, -4.5)
#2 %s%eexception_handling%eexception_class.php(140): fL1(10)
#3 {main}<
getMessage: >fL3 Message<
getCode: >345<
getPrevious: ><
getFile: >%s%eexception_handling%eexception_class.php<
getLine: >107<
getTraceAsString: >#0 %s%eexception_handling%eexception_class.php(95): fL3('xyz', NULL, true)
#1 %s%eexception_handling%eexception_class.php(69): fL2(2.3, -4.5)
#2 %s%eexception_handling%eexception_class.php(140): fL1(10)
#3 {main}<
array(3) {
[0]=>
array(4) {
["file"]=>
string(%d) "%s%eexception_handling%eexception_class.php"
["line"]=>
int(95)
["function"]=>
string(3) "fL3"
["args"]=>
array(3) {
[0]=>
%Sstring(3) "xyz"
[1]=>
%SNULL
[2]=>
%Sbool(true)
}
}
[1]=>
array(4) {
["file"]=>
string(%d) "%s%eexception_handling%eexception_class.php"
["line"]=>
int(69)
["function"]=>
string(3) "fL2"
["args"]=>
array(2) {
[0]=>
%Sfloat(2.3)
[1]=>
%Sfloat(-4.5)
}
}
[2]=>
array(4) {
["file"]=>
string(%d) "%s%eexception_handling%eexception_class.php"
["line"]=>
int(140)
["function"]=>
string(3) "fL1"
["args"]=>
array(1) {
[0]=>
%Sint(10)
}
}
}
Trace Info:
Key[0]:
Key[file] => >%s%eexception_handling%eexception_class.php<
Key[line] => >95<
Key[function] => >fL3<
Key[args]:
Key[0] => >xyz<
Key[1] => ><
Key[2] => >1<
Key[1]:
Key[file] => >%s%eexception_handling%eexception_class.php<
Key[line] => >69<
Key[function] => >fL2<
Key[args]:
Key[0] => >2.3<
Key[1] => >-4.5<
Key[2]:
Key[file] => >%s%eexception_handling%eexception_class.php<
Key[line] => >140<
Key[function] => >fL1<
Key[args]:
Key[0] => >10<
fL3: In finally-block
fL3: Beyond try/catch/finally blocks
==========