# RFC: Multi catch

- Status: Implemented (PHP 7.1)
- Authors: Pierrick Charron pierrick@php.net, Bronisław Białek after89@gmail.com
- Date: 2016-03-06
- Version: 1.0
- Original: https://wiki.php.net/rfc/multiple-catch
- Page: https://externals.io/rfc/multiple-catch
- 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

### Should milti-catch be added to PHP 7.1 (closed)

Yes: 40 · No: 6

Voters: bishop (Yes), blanchonvincent (Yes), bwoebi (Yes), colinodell (Yes), danack (Yes), daverandom (Yes), davey (Yes), derick (No), dmitry (Yes), francois (Yes), galvao (Yes), guilhermeblanco (Yes), hywan (Yes), jhdxr (Yes), jpauli (Yes), jwage (Yes), kalle (Yes), klaussilveira (Yes), laruence (No), lcobucci (No), leigh (Yes), levim (Yes), lstrojny (Yes), marcio (Yes), mariano (Yes), mbeccati (Yes), mcmic (Yes), mightyuhu (Yes), mike (Yes), ocramius (No), patrickallaert (No), pauloelr (Yes), pierrick (Yes), pollita (Yes), ramsey (Yes), rmf (Yes), sammyk (Yes), santiagolizardo (Yes), stas (Yes), svpernova09 (Yes), thorstenr (Yes), trowski (Yes), tyrael (Yes), weierophinney (Yes), zeev (Yes), zimt (No)

## Discussions on the mailing list

- [\[RFC Discussion\] Catching multiple exception types](https://externals.io/message/91555.md) — started on 2016-03-08, 19 messages
- [\[VOTE\] Catching Multiple Exception Types](https://externals.io/message/92388.md) — started on 2016-04-17, 33 messages
- [\[RFC\]\[Accepted\] Catching Multiple Exception Types](https://externals.io/message/93025.md) — started on 2016-05-01, 1 messages

---

## Introduction

When two different types of exceptions are handled the same way, it is sometimes required to duplicate the code of the `catch` statements.

For example :

```php
<?php
 
try {
   // Some code...
} catch (ExceptionType1 $e) {
   // Code to handle the exception
} catch (ExceptionType2 $e) {
   // Same code to handle the exception
} catch (Exception $e) {
   // ...
}
```

A solution to fix this problem on the user level would be to implement a common interface for `ExceptionType1` and `ExceptionType2` and catch it. However, this is only possible when you control the exception hierarchy in your own code, but not possible when you don't control the code.

## Proposal

This RFC introduces the possibility to catch multiple exception types in a single `catch` statement to avoid code duplication.

```php
<?php
 
try {
   // Some code...
} catch (ExceptionType1 | ExceptionType2 $e) {
   // Code to handle the exception
} catch (\Exception $e) {
   // ...
}
```

This syntax and functionality is compatible with the Union types RFC proposed here : [https://wiki.php.net/rfc/union\_types](https://externals.io/rfc/union_types)

### When is it useful ?

- When a single method/function throw two different types of exceptions that you handle the same way;
- When libraries are leaking their dependency's exceptions.

## Backward Incompatible Changes

None.

## Proposed PHP Version(s)

7.1

## RFC Impact

### To SAPIs

None

### To Existing Extensions

None

### To Opcache

None

### To PHP tools/IDE

This change will require every PHP tools and IDE based on an AST parser to be modified to take this syntax change into account.

## Links and Literature

Similar feature in Java: <http://docs.oracle.com/javase/7/docs/technotes/guides/language/catch-multiple.html>

Request on StackOverflow: <http://stackoverflow.com/questions/8439581/catching-multiple-exception-types-in-one-catch-block>

## Vote

As this is a language change, a 2/3 majority is required. The vote is a straight Yes/No vote for accepting the RFC and merging the patch. Voting started on 2016-04-17 and will end on 2016-05-01.

**Vote: Should milti-catch be added to PHP 7.1** — Yes: 40 · No: 6 (closed)

## Patches and Tests

Patch : <https://github.com/php/php-src/pull/1796/files>

## Implementation

Merged into php-src for PHP 7.1: <https://github.com/php/php-src/commit/0aed2cc2a440e7be17552cc669d71fdd24d1204a>
