rfc:multiple-catch

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:multiple-catch [2016/03/07 18:22] pierrickrfc:multiple-catch [2017/09/22 13:28] (current) – external edit 127.0.0.1
Line 2: Line 2:
   * Version: 1.0   * Version: 1.0
   * Date: 2016-03-06   * Date: 2016-03-06
-  * Author: Pierrick Charronpierrick@php.net, Bronisław Białek after89@gmail.com +  * Author: Pierrick Charron <pierrick@php.net>, Bronisław Białek <after89@gmail.com> 
-  * Status: Draft+  * Status: Implemented (PHP 7.1)
   * First Published at: http://wiki.php.net/rfc/multiple-catch   * First Published at: http://wiki.php.net/rfc/multiple-catch
  
 ===== Introduction ===== ===== Introduction =====
  
-It is sometime required to duplicate code of a catch statement because two totally different types of Exceptions are handled the same way.+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 : 
  
 <code php> <code php>
Line 19: Line 21:
 } catch (ExceptionType2 $e) { } catch (ExceptionType2 $e) {
    // Same code to handle the exception    // Same code to handle the exception
-} catch (\Exception $e) {+} catch (Exception $e) {
    // ...    // ...
 } }
 </code> </code>
 +
 +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 ===== ===== 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.+This RFC introduces the possibility to catch multiple exception types in a single ''catch'' statement to avoid code duplication.
  
 <code php> <code php>
Line 40: Line 44:
 </code> </code>
  
-This syntax and functionality is compatible with the Union RFC proposed here : https://wiki.php.net/rfc/union_types+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. 
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
Line 59: Line 69:
 ==== To Opcache ==== ==== To Opcache ====
 None 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.
 +
 +<doodle title="Should milti-catch be added to PHP 7.1" auth="pierrick" voteType="single" closed="true">
 +   * Yes
 +   * No
 +</doodle>
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
  
-Patch : [[https://github.com/php/php-src/compare/master...adoy:multiple-catch]]+Patch : [[https://github.com/php/php-src/pull/1796/files]] 
 + 
 +===== Implementation ===== 
 + 
 +Merged into php-src for PHP 7.1https://github.com/php/php-src/commit/0aed2cc2a440e7be17552cc669d71fdd24d1204a
  
rfc/multiple-catch.1457374934.txt.gz · Last modified: 2017/09/22 13:28 (external edit)