© Mikael Olsson 2021
M. OlssonPHP 8 Quick Scripting Referencehttps://doi.org/10.1007/978-1-4842-6619-9_30

30. Exception Handling

Mikael Olsson1  
(1)
Hammarland, Finland
 

PHP 5 introduced exceptions, a built-in mechanism for handling program failures within the context in which they occur. Unlike errors, which generally need to be fixed by the developer, exceptions are handled by the script. They represent an irregular run-time situation that should have been expected as a possibility and which the script should be able to handle on its own.

Try-catch Statement

To handle an exception, it must be caught using a try-catch statement. This statement consists of a try block containing the code that may cause the exception, and one or more catch clauses

Get PHP 8 Quick Scripting Reference: A Pocket Guide to PHP Web Scripting 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.