PHP RFC: non-capturing catches
- Version: 0.9
- Date: 2020-04-05
- Author: Max Semenik maxsem.wiki@gmail.com
- Status: Implemented in PHP 8.0
- Implementation: https://github.com/php/php-src/pull/5345
- First Published at: https://wiki.php.net/rfc/non-capturing_catches
Introduction
Currently, PHP requires to capture the exception being caught to a variable:
try { foo(); } catch (SomeException $ex) { die($ex->getMessage()); }
However, you need to specify the variable even if it's not used:
try { changeImportantData(); } catch (PermissionException $ex) { echo "You don't have permission to do this"; }
Someone reading the above code is left to wonder if the author intended to not use the exception variable or it's a bug.
Proposal
Allow catching exceptions without capturing them to variables:
try { changeImportantData(); } catch (PermissionException) { // The intention is clear: exception details are irrelevant echo "You don't have permission to do this"; }
Prior art
Such change was first proposed in this RFC 7 years ago. It was turned down mostly because it also wanted to allow blanket catches like this:
try { foo(); } catch { bar(); }
Meanwhile what I'm proposing here has received mostly positive feedback so I feel it's worth revisiting.
Backward Incompatible Changes
None.
Proposed PHP Version(s)
8.0
RFC Impact
None.
Vote
Voting started on 2020-05-10 and will end on 2020-05-24 at 9:00 UTC.
Patches and Tests
Implementation
After the project is implemented, this section should contain
- the version(s) it was merged into
- a link to the git commit(s)
- a link to the PHP manual entry for the feature
- a link to the language specification section (if any)
References
* https://externals.io/message/109550 - initial announcements
Rejected Features
Keep this updated with features that were discussed on the mail lists.