3.6. Exceptions

When errors occur, PHP sends text to the browser. Some errors halt execution. For the error conditions that don't halt execution, you may trap them with a function you register with set_error_handler. See Chapter 15 for a discussion of this function. You can even generate your own errors with trigger_error, discussed in Chapter 9.

Alternatively, you may use exceptions. Figure 3.7 shows the form. Exceptions are object-oriented error conditions. They occur within the context of a try statement. To initiate an exception, you make a throw statement. Control then passes to a catch block, which receives a copy of the thrown exception. Add a catch block for each type of exception you wish to catch, or simply use PHP's built-in Exception ...

Get Core PHP Programming, 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.