Menu

← Back to throw.*

Error Identifier: throw.notSupported

Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.

Code example #

<?php declare(strict_types = 1);

function doFoo(bool $condition): string
{
    $value = $condition ? 'hello' : throw new \Exception('Not valid');
}

Why is it reported? #

Throw expressions (using throw as an expression rather than a statement) were introduced in PHP 8.0. In earlier versions of PHP, throw can only be used as a standalone statement. Using throw inside a ternary expression, null coalescing operator, or short closure is a syntax error on PHP versions before 8.0.

This error is reported when PHPStan is configured to analyse code for a PHP version that does not support throw expressions.

How to fix it #

If the project requires compatibility with PHP versions before 8.0, rewrite the code to use throw as a statement:

 <?php declare(strict_types = 1);
 
 function doFoo(bool $condition): string
 {
-    $value = $condition ? 'hello' : throw new \Exception('Not valid');
+    if (!$condition) {
+        throw new \Exception('Not valid');
+    }
+    $value = 'hello';
 }

Non-ignorable error #

This error cannot be ignored using @phpstan-ignore or the ignoreErrors configuration. Non-ignorable errors indicate code that would cause a crash or a fatal error at runtime, or a fundamental problem in the analysed code that must be addressed.

Rules that report this error #

  • PHPStan\Rules\Exceptions\ThrowExpressionRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.