Unless the code in the catch block rethrows the exception, it is assumed that it handled
the exception, and the execution of your script continues normally. This kind of flexibility allows you to prevent
many causes that could make your pages stop working, and you??™ll appreciate the power exceptions give
you when writing PHP code!
A PHP 5 exception is represented by the Exception class, which contains the exception??™s details. You
can generate (throw) an exception yourself using the throw keyword. The Exception object that you throw
is propagated through the call stack until it is intercepted using the catch keyword. The call stackis the list
CHAPTER 4 ?– CREATING THE PRODUCT CATALOG: PART 1 88
of methods being executed. So if a function A() calls a function B(), which in turn calls a function C(), then
the call stack will be formed of these three methods. In this scenario, an exception that is raised in function
C() can be handled in the same function, provided the offending code is inside a try-catch block. If this is
not the case, the exception propagates to method B(), which has a chance to handle the exception, and so
on. If no method handles the exception, the exception is finally intercepted by the PHP interpreter, which
transforms the exception into a PHP Fatal Error.
In our database-handling code, we??™ll catch the potential exceptions that could be generated by PDO.
Pages:
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