Managing Error Conditions with Exceptions

Although it was possible to set a custom error handler in PHP 4, most scripts prior to PHP 5 made do with relatively crude error handling. Methods that ran into trouble would generally choose between using die() to end script execution completely or returning a flag value such as false or -1.

PHP 5 introduces exceptions, allowing methods to hand responsibility back to client code when particular errors are encountered.

An exception is an object that can be automatically populated with information about the error that has occurred and its script context. You must instantiate Exception objects yourself with the new keyword just as you would with any object. Once you have an Exception object, you can literally ...

Get Sams Teach Yourself PHP in 24 Hours, Third Edition 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.