Non-capturing catches View as Markdown

Implemented (PHP 8.0)
Authors
Max Semenik maxsem.wiki@gmail.com
Date
2020-04-05
Version
0.9
Implementation
https://github.com/php/php-src/pull/5345
Source
wiki.php.net/rfc/non-capturing_catches

Vote

Accept this RFC? closed
Yes
48
No
1
49 voters
alcaeus Yes 2020-05-11 10:39
asgrim Yes 2020-05-13 14:02
ashnazg Yes 2020-05-13 12:10
beberlei Yes 2020-05-23 13:34
bmajdak Yes 2020-05-10 17:06
bwoebi Yes 2020-05-10 10:26
carusogabriel Yes 2020-05-11 11:02
cmb Yes 2020-05-10 13:04
dams Yes 2020-05-11 13:26
dmitry Yes 2020-05-18 08:41
duncan3dc Yes 2020-05-10 11:45
ekin Yes 2020-05-13 20:51
galvao Yes 2020-05-10 15:55
geekcom Yes 2020-05-15 03:17
jasny Yes 2020-05-15 13:44
jbnahan Yes 2020-05-11 05:07
jhdxr Yes 2020-05-10 21:55
kalle Yes 2020-05-10 11:17
kelunik Yes 2020-05-19 21:09
kguest Yes 2020-05-10 08:41
kocsismate Yes 2020-05-10 18:06
levim Yes 2020-05-18 02:06
lstrojny Yes 2020-05-15 11:47
marandall Yes 2020-05-10 18:39
marcio Yes 2020-05-15 22:06
mariano Yes 2020-05-10 11:14
mbeccati Yes 2020-05-13 07:09
mcmic Yes 2020-05-13 09:54
mike Yes 2020-05-12 13:12
nicolasgrekas Yes 2020-05-13 13:59
nikic Yes 2020-05-11 09:17
ocramius Yes 2020-05-13 09:15
pierrick Yes 2020-05-18 19:07
pmjones Yes 2020-05-10 23:05
pmmaga Yes 2020-05-11 12:43
pollita Yes 2020-05-15 14:32
rtheunissen Yes 2020-05-15 19:55
ruudboon Yes 2020-05-16 16:51
salathe Yes 2020-05-22 09:29
santiagolizardo Yes 2020-05-15 13:21
sergey Yes 2020-05-10 08:35
sirsnyder Yes 2020-05-11 01:03
svpernova09 Yes 2020-05-15 12:40
tandre Yes 2020-05-10 14:00
trowski Yes 2020-05-21 17:16
wjx No 2020-05-11 04:02
wyrihaximus Yes 2020-05-14 10:57
yunosh Yes 2020-05-11 07:01
zimt Yes 2020-05-15 08:44

Discussions on the mailing list

[RFC] Non-capturing catches
by Max Semenik · 3 messages
[RFC] VOTE: Non-capturing catches
by Max Semenik · 2 messages

RFC text

Copied from wiki.php.net, last synchronized Oct 13, 2026 11:51 UTC

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.

Vote: Accept this RFC? — Yes: 48 · No: 1 (closed)

Patches and Tests

* https://github.com/php/php-src/pull/5345

Implementation

After the project is implemented, this section should contain

  1. the version(s) it was merged into
  2. a link to the git commit(s)
  3. a link to the PHP manual entry for the feature
  4. 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.