Chapter 7. Exception Handling

In this chapter, we explain the concept of exception handling and the way it is implemented in PHP. Exceptions are a new and important feature in PHP5. They provide a unified mechanism for handling errors in an extensible, maintainable, and object-oriented way.

Key topics covered in this chapter include

  • Exception handling concepts

  • Exception control structures: try...throw...catch

  • The Exception class

  • User-defined exceptions

  • Exceptions in Bob's Auto Parts

  • Exceptions and PHP's other error handling mechanisms

Exception Handling Concepts

The basic idea of exception handling is that code is executed inside what is called a try block. That's a section of code that looks like this:

try
{
  // code goes here
}

If something goes wrong ...

Get PHP and MySQL Web Development, 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.