Version: 1.0
Date: 2016-03-06
Status: Implemented (PHP 7.1)
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 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.
This RFC introduces the possibility to catch multiple exception types in a single catch
statement to avoid code duplication.
<?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
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.
None.
Proposed PHP Version(s)7.1
RFC Impact To SAPIsNone
To Existing ExtensionsNone
To OpcacheNone
To PHP tools/IDEThis 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 VoteAs 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.
Patches and TestsPatch : https://github.com/php/php-src/pull/1796/files
ImplementationMerged into php-src for PHP 7.1: https://github.com/php/php-src/commit/0aed2cc2a440e7be17552cc669d71fdd24d1204a
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.3