Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Language Constructs and Statements

PHP try, catch and finally

Reference for PHP try, catch and finally: handle exceptions and errors, multi-catch, rethrowing, catch without a variable, finally semantics and return overrides.

try wraps code that might throw. If something inside throws an exception (or an Error), PHP looks through the following catch blocks in order and runs the first one whose type matches. An optional finally block runs afterward no matter what happened: success, caught exception, uncaught exception or early return.

Both Exception and Error implement Throwable, so catch (Throwable $e) catches everything, including type errors and division by zero from intdiv(). Warnings and notices are not exceptions unless you convert them with an error handler.

PHP
try {
    risky();
} catch (InvalidArgumentException | RangeException $e) {
    echo $e->getMessage();
} finally {
    echo "cleanup";
}

Try it

PHP Catching, multi-catch and finally

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Rethrowing, chaining and finally with return

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
try {
    // code that may throw
} catch (TypeA | TypeB $e) {
    // handle
} catch (Throwable) {
    // PHP 8.0+: variable is optional
} finally {
    // always runs
}
Part Description
try Required block. Must be followed by at least one catch or a finally.
catch (Type $e) Runs when the thrown object is an instance of Type. Several types are separated by `
finally Optional. Runs after try and any catch, even when they return or throw.

Return value: none; it is a statement, but return inside it returns from the function.

Usage notes

  • Order matters. A catch (Exception $e) placed before catch (InvalidArgumentException $e) makes the second unreachable.
  • Exception versus Error. catch (Exception $e) does not catch TypeError, ValueError, DivisionByZeroError or ArgumentCountError; those extend Error. Catch Throwable only at boundaries such as a top-level handler.
  • Exception chaining. Pass the original as the third constructor argument and read it with getPrevious() so the cause is not lost.
  • finally overrides. A return or throw inside finally replaces the pending result or exception, which hides bugs. Avoid it.
  • Resource cleanup. Close files and release locks in finally, or use RAII-like wrappers.
  • Uncaught exceptions stop the script with a fatal error unless set_exception_handler() is registered.
  • Cost. Creating exceptions captures a stack trace; do not use them for ordinary control flow such as "loop end".
  • Catch without variable and multi-catch need PHP 8.0 and 7.1 respectively.
  • To raise exceptions, see throw.