# RFC: Non-capturing catches

- Status: 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
- Original: https://wiki.php.net/rfc/non-capturing_catches
- Page: https://externals.io/rfc/non-capturing_catches
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

## Votes

### Accept this RFC? (closed)

Yes: 48 · No: 1

Voters: alcaeus (Yes), asgrim (Yes), ashnazg (Yes), beberlei (Yes), bmajdak (Yes), bwoebi (Yes), carusogabriel (Yes), cmb (Yes), dams (Yes), dmitry (Yes), duncan3dc (Yes), ekin (Yes), galvao (Yes), geekcom (Yes), jasny (Yes), jbnahan (Yes), jhdxr (Yes), kalle (Yes), kelunik (Yes), kguest (Yes), kocsismate (Yes), levim (Yes), lstrojny (Yes), marandall (Yes), marcio (Yes), mariano (Yes), mbeccati (Yes), mcmic (Yes), mike (Yes), nicolasgrekas (Yes), nikic (Yes), ocramius (Yes), pierrick (Yes), pmjones (Yes), pmmaga (Yes), pollita (Yes), rtheunissen (Yes), ruudboon (Yes), salathe (Yes), santiagolizardo (Yes), sergey (Yes), sirsnyder (Yes), svpernova09 (Yes), tandre (Yes), trowski (Yes), wjx (No), wyrihaximus (Yes), yunosh (Yes), zimt (Yes)

## Discussions on the mailing list

- [\[RFC\] Non-capturing catches](https://externals.io/message/109550.md) — started on 2020-04-07, 3 messages
- [\[RFC\] VOTE: Non-capturing catches](https://externals.io/message/110104.md) — started on 2020-05-10, 2 messages

---

## Introduction

Currently, PHP requires to capture the exception being caught to a variable:

```php
try {
    foo();
} catch (SomeException $ex) {
    die($ex->getMessage());
}
```

However, you need to specify the variable even if it's not used:

```php
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:

```php
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](https://externals.io/rfc/anonymous_catch) 7 years ago. It was turned down mostly because it also wanted to allow blanket catches like this:

```php
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.
