rfc:multiple-catch
This is an old revision of the document!
PHP RFC: Catching Multiple Exception Types
- Version: 1.0
- Date: 2016-03-06
- Author: Pierrick Charron, pierrick@php.net, Bronisław Białek after89@gmail.com
- Status: Draft
- First Published at: http://wiki.php.net/rfc/multiple-catch
Introduction
It is sometime required to duplicate code of a catch statement because two totally different types of Exceptions are handled the same way.
<?php try { // Some code... } catch (ExceptionType1 $e) { // Code to handle the exception } catch (ExceptionType2 $e) { // Same code to handle the exception } catch (\Exception $e) { // ... }
Proposal
This RFC introduce the possibility to catch multiple exceptions type in a single catch statement. It will avoid code duplication when similar code is reused across multiple exception types. The same thing could be done by implementing a common interface for ExceptionType1 and ExceptionType2 but you don't always have the control on exceptions structure if you're using external code.
<?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 RFC proposed here : https://wiki.php.net/rfc/union_types
Backward Incompatible Changes
None.
Proposed PHP Version(s)
7.1
RFC Impact
To SAPIs
None
To Existing Extensions
None
To Opcache
None
Patches and Tests
rfc/multiple-catch.1457374934.txt.gz · Last modified: 2025/04/03 13:08 (external edit)