Psalm: Catch classes should implement Throwable or extend Exception

Created on 28 Dec 2017  路  11Comments  路  Source: vimeo/psalm

class A {}

try {
  echo "hello";
} catch (A $a) {}

should fail with InvalidCatch

bug

Most helpful comment

@ndench @Wirone I've just made both InvalidClass and InvalidCatch both configurable by class.

All 11 comments

A note about fixing occurences of InvalidCatch: InvalidCatch may be seen when a class implements an interface (my_interface_exception).

Zend PHP allows you to have catch (my_interface_exception $e) {}, and will catch an interface.
Aside: HHVM doesn't allow you to do that.

This can easily be solved in PHP7 by adding interface my_interface_exception extends Throwable if my_interface_exception isn't already throwable.

Also, docblock suppression needs to be on the try keyword (also shown by the test above).

@muglug why does psalm fail with InvalidCatch when catching a random interface? Shouldn't that be allowed?

@ndench that interface should extend \Throwable. It's known issue, for example with GuzzleException (interface).

@muglug is it possible to suppress InvalidCatch by configuration? referencedClass does not work.

@ndench @Wirone I've just made both InvalidClass and InvalidCatch both configurable by class.

This can easily be solved in PHP7 by adding interface my_interface_exception extends Throwable if my_interface_exception isn't already throwable.

My earlier comment was incorrect: classes/interfaces can't (EDIT: directly) implement throwable, that causes a PHP error. Classes can only (EDIT: indirectly implement Throwable if they) extend Error/Exception

Classes can implement Throwable (or interfaces extending that) if they also extend Exception/Error.

Yeah, the exception I allow via the config is for 5.6-compatible exception-like interfaces. But I very much hope it鈥檚 a dwindling need

@muglug great news! when will it be released?

There was a 2.x release the day after - sorry, should have pinged this issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

orklah picture orklah  路  3Comments

vudaltsov picture vudaltsov  路  3Comments

tux-rampage picture tux-rampage  路  3Comments

muglug picture muglug  路  3Comments

roukmoute picture roukmoute  路  3Comments