-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathch12p2Exceptions.php
113 lines (110 loc) · 2.92 KB
/
ch12p2Exceptions.php
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
<?php
include_once('generalIncludes.php');
echo '<input id="chapter" type="hidden" value="12">';
echo '<h2>Chapter 12 - paragraph Exceptions</h2>';
//echo '<h3>Listing 12.2: The base Exception class</h3>';
//showcode(<<<'CODE'
//class Exception
//{
//// The error message associated with this exception
// protected $message = 'Unknown Exception';
//// The error code associated with this exception
// protected $code = 0;
//// The pathname of the file where the exception occurred
// protected $file;
//// The line of the file where the exception occurred
// protected $line;
//// Constructor
// function __construct ($message = null, $code = 0){}
//// Returns the message
// final function getMessage(){}
//// Returns the error code
// final function getCode(){}
//// Returns the file name
// final function getFile(){}
//// Returns the file line
// final function getLine(){}
//// Returns an execution backtrace as an array
// final function getTrace(){}
//// Returns a backtrace as a string
// final function getTraceAsString(){}
//// Returns a string representation of the exception
// function __toString(){}
//}
//CODE
//);
echo '<h3>Listing 12.3: Bubbling exception through try...catch</h3>';
showcode(<<<'CODE'
try {
if (true) {
echo "Throwing\n";
throw new \Exception("This is my error");
}
} catch (\Exception $e) {
echo "Catching Exception: " . $e->getMessage();
}
CODE
);
echo '<h3>Listing 12.4: Extending the base Exception class</h3>';
showcode(<<<'CODE'
//namespace myCode;
class myException extends \Exception { }
try {
try {
try {
new PDO("mysql:dbname=zce");
throw new myException("An unknown error occurred.");
} catch (\PDOException $e) {
echo $e->getMessage();
}
} catch(\myCode\Exception $e) {
echo $e->getMessage();
}
} catch (\Exception $e) {
echo $e->getMessage();
}
CODE
);
echo '<h3>Listing 12.5: Catching different exceptions</h3>';
showcode(<<<'CODE'
try {
new PDO("mysql:dbname=zce");
throw new myException("An unknown error occurred.");
} catch (\PDOException $e) {
echo $e->getMessage();
} catch (\myCode\Exception $e) {
echo $e->getMessage();
} catch (\Exception $e) {
echo $e->getMessage();
}
CODE
);
echo '<h3>Listing 12.6: Handling uncaught exceptions</h3>';
showcode(<<<'CODE'
function handleUncaughtException($e) {
echo $e->getMessage();
}
set_exception_handler("handleUncaughtException");
//throw new Exception("You caught me!");
echo "If throw gets uncommented, this message will not be displayed";
CODE
);
echo '<h3>Listing 12.7: Using a finally block</h3>';
showcode(<<<'CODE'
try {
// Try something
echo "Trying something\n";
} catch (\Exception $exception) {
// Handle exception
echo "Catching it\n";
throw new Exception("You caught me!");
} finally {
// Whatever happened, do this
echo "Finalizing it\n";
}
CODE
);
echo '<h3></h3>';
showcode(<<<'CODE'
CODE
);