PHP has an outstanding error and exception handling. Its correct implementation is key in modern high quality, fool-proof, fail-safe software applications. Either the System decides what to do in case of an exception, so to not stop execution of the process, or a message telling the Client what is happening is sent back.



PHP Errors are not the same as Exceptions. Errors are reported by the PHP interpreter at execution time and are mostly caused by internal error conditions.

There are 2 directives in the php.ini file that are configured to either output or log them and to decide the level of error reporting. For more info on this go to php language errors

 All internal PHP Functions are using error reporting and only the Object Oriented PHP Extensions are using Exceptions. Although there is a way to easily turn errors into exceptions with ErrorException its more common to just log PHP internal errors for taking care of them in other environment but in production. Also, a healthy PHP codebase shouldn't be reporting any errors in production.

Exceptions are objects that are handled by a try-catch model similar as many other modern object oriented programming languages and are very useful to get insight on the error code, the error message, the file and a trace from the start of the script to the line where the exception happened.

The most common way to implement an exception is like this:

        try {
            // some process here
        } catch (\Throwable $e) {
            throw new PreconditionFailedException($e->getMessage());

 Its important to notice that all Exceptions must implement Throwable Interface and you can create your own Exceptions extending other Exceptions or the Exception class itself:

class PreconditionFailedException extends HttpException
    public const MESSAGE_PREFIX = '';

     * @param $message
    public function __construct($message)
        parent::__construct(ErrorCodes::HTTP_412, self::MESSAGE_PREFIX . $message);

     * @return mixed
    public function getApiException()
        return $this;

In this case, this exception is sending back a message to the client and an HTTP Status 412 telling the Client that a Response cannot be produced due to a bad Request.

There are a few Exceptions already available in PHP that are extending the basic Exception class. You can use them directly, or create a new Exception Class extending any of these.

The following image shows a dump of the full exception object content produced when no User found for the email provided:


Exceptions are very powerful tool for Developers. You can use them for both, sending back to the Client a short useful message and a HTTP Status code (In the case of working in REST), and at Development time, to easily locate the file and the line that is causing the exception to happen.

A High Quality Software Application must return an Exception for each one of all possible failed use cases, because all clients are either expecting a successful response with an HTTP status code of 200, or an Exception message and the HTTP Status code.

For more on this refer to PHP Exceptions.