Skip to content

Chapter 16 of 38

Error and Exception Handling

Use Throwable, exceptions, handlers, finally, logging, and production-safe reporting.

38 minutes 10 quick checksBy Subha Prasad
Lesson 16 of 38Course navigation

Lesson content

Read, practise, then check your understanding

Error and Exception implement Throwable. Catch specific failures where recovery, translation, or contextual reporting is possible; preserve the original throwable.

Practical example

<?php
try { $config = loadConfig($path); }
catch (JsonException $error) { throw new RuntimeException('Configuration is invalid', 0, $error); }
finally { /* unconditional cleanup */ }

Production should log rather than display internal errors. A top-level handler can format uncaught failures, while transaction and resource code still needs local cleanup. Avoid empty catches, sensitive log data, and converting every warning without policy.

Knowledge check

Answer every question correctly to complete this chapter.

Which statement best describes Throwable?
Which PHP term matches this description: The common interface implemented by Error and Exception.
Which statement best describes try?
Which PHP term matches this description: A block whose thrown failures may be caught.
Which statement best describes catch?
Which PHP term matches this description: A typed handler for a matching thrown object.
Which statement best describes finally?
Which PHP term matches this description: A block run after try/catch for unconditional cleanup.
Which statement best describes error handler?
Which PHP term matches this description: A callback registered to translate or report selected PHP errors.

0 of 10 checks passed

Your progress is saved on this device.

Error and Exception Handling | PHP Lesson | Subha Prasad