Prev | Current Page 158 | Next

Emilian Balanescu and Cristian Darie

"Beginning PHP and MySQL E-Commerce: From Novice to Professional, Second Edition"


Having those details, a programmer can fix the code to avoid the same error happening in the future.
PHP 5 introduced, along with other object-oriented programming (OOP) features, a new way to handle
runtime errors: enter exceptions. Exceptions represent the modern way of managing runtime errors in your
code and are much more powerful and flexible than PHP errors. Exceptions are a very important part of the
OO (object oriented) model, and PHP 5 introduces an exception model resembling that of other OOP languages,
such as Java and C#. However, exceptions in PHP coexist with the standard PHP errors in a strange combination,
and you can??™t solely rely on exceptions for dealing with runtime problems. Some PHP extensions, such
as PDO, can be configured to generate exceptions to signal problems that happen at runtime, whereas in
other cases, your only option is to deal with standard PHP errors.
The advantages of exceptions over errors lay in the flexibility you??™re offered in handling them. When an
exception is generated, you can handle it locally and let your script continue executing normally, or you can
pass the exception to another class for further processing. With exceptions, your script isn??™t terminated like it
is when a PHP error appears. When using exceptions, you place the code that you suspect could throw an
exception inside a try block and handle potential exceptions in an associated catch block:
try
{
// Code that could generate an exception that you want to handle
}
catch (Exception $e)
{
// Code that is executed when an exception is generated
// (exception details are accessible through the $e object)
}
When an exception is generated by any of the code in the try block, the execution is passed directly to
the catch block.


Pages:
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
Brian McKnight Modest Mouse Pat Metheny Mireille Mathieu Dorota Miśkiewicz