Catching Exceptions

You have now finished all the changes in your database classes that relate to exceptions. All you need to do now is to see how exceptions are caught by enclosing your code within a try block.

A try block is a programming structure that is used to enclose code that may cause errors. It is always followed by a catch block. An error, or more properly speaking an exception, that occurs within the try is thrown and handled by the catch. This is why a try/catch block is said to handle exceptions.

However, there are important differences between error trapping and exception handling. The argument to a catch clause is always an object. Any Exception that occurs within the scope of the try block will look for a catch that has a matching ...

Get Object-Oriented PHP now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.