Search code examples
phpexceptiontry-catchthrowphpstan

PHPSTAN : catch an exception without throwing it


I everyone, I have this simple try catch snippet, written in PHP :

    try {
      ...
      // some code
      ...
    } catch (Throwable $e) {
        $response = ['message' => $e->getMessage(), 'trace' => $e->getTraceAsString()];
    }

    return $response;

This code works good, but PHPSTAN does not think it is well-done :

caught "Throwable" must be rethrown. Either catch a more specific exception or add a "throw" clause in the "catch" block to propagate the exception.

I understand the message, but I wonder if it is anyway possible to catch an exception whitout throwing it, and still validate phpstan checks ?

Thank you very much for reading and if you have a clue, I take it!


Solution

  • This is coming from thecodingmachine/phpstan-strict-rules which you must have installed. If you're not interested in this rule, you can uninstall the package.