Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Language Constructs and Statements

PHP throw statement and expression

Reference for PHP throw: raise an exception with a statement or, since PHP 8.0, as an expression in ??, ternaries and arrow functions, plus rethrowing.

throw raises an exception. Execution stops at that point and PHP looks for the nearest matching catch block up the call stack. If none is found, the script ends with a "Fatal error: Uncaught ..." message.

You can throw any object that implements Throwable: instances of Exception, Error or your own subclasses. Since PHP 8.0 throw is an expression, so it can appear where a value is expected, for example on the right side of ?? or inside an arrow function.

PHP
throw new InvalidArgumentException("Age must be positive");

$name = $input ?? throw new RuntimeException("Missing name");

Try it

PHP Throwing and catching

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 throw as an expression

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
throw expression;
Name Description
expression An expression that evaluates to an object implementing Throwable. Usually new SomeException($message, $code, $previous).

Return value: none. Control never continues after the throw. As an expression its type is never.

Errors: throwing something that is not a Throwable (a string, an int, an object of another class) raises an Error with the message "Can only throw objects".

Usage notes

  • Statement or expression. Before PHP 8.0 throw was only a statement, so $x ?? throw ... and fn() => throw ... were syntax errors. Now both work, and so does the ternary form $ok ? $value : throw new Exception().
  • Throw objects only. Unlike some languages, PHP cannot throw strings or numbers. Create an exception class when you need a new kind of failure; see Custom exceptions.
  • Chain the cause. Pass the original exception as the third constructor argument, as in the second example, and read it back with getPrevious(). This keeps the low-level trace when you translate errors between layers.
  • Rethrowing. Inside a catch block, throw $e; rethrows the same object and keeps its original file, line and trace, because those are fixed when the object is created.
  • Choose the right class. Use InvalidArgumentException or ValueError for bad input, RuntimeException for failures only known at run time, and LogicException for programmer mistakes.
  • Don't use exceptions for ordinary flow. They are meant for exceptional situations; a missing optional value is better handled with null or a default.
  • Uncaught exceptions are passed to the handler set with set_exception_handler(); without one they end the script with a fatal error.
  • finally still runs when a throw leaves a try block.