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 zeroNotes
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
- Exception — The base class of exceptions.
- Error — The base class of PHP's internal errors.
- set_exception_handler — Sets a function for exceptions nobody caught.
Related recipes
In the tutorial
- Exceptions — Exceptions in PHP — throw, try, catch and finally, custom exception classes and built-in errors.