Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

Throwable

The common interface of everything that can be thrown and caught.

Syntax

catch (Throwable $e) { … }

Return value

ThrowableAn interface with the `getMessage()`, `getCode()`, `getFile()`, `getLine()`, `getTrace()` and `getPrevious()` methods.

Examples

Catching both an exception and an error:

<?php
 
function risky(int $case): void
{
    if ($case === 1) {
        throw new RuntimeException('exception');
    }
 
    intdiv(1, 0);
}
 
foreach ([1, 2] as $case) {
    try {
        risky($case);
    } catch (Throwable $e) {
        echo get_class($e), ': ', $e->getMessage(), "\n";
    }
}

Output:

RuntimeException: exception
DivisionByZeroError: Division by zero

Notes

catch (Exception $e) does not catch TypeError or DivisionByZeroError — they extend Error. Throwable catches everything; use it at the top level of a program.

See also

Related recipes

In the tutorial

  • Exceptions — Exceptions in PHP — throw, try, catch and finally, custom exception classes and built-in errors.

Thanks for reading!

Donate to the site — we work hard for you.