rfc:non-capturing_catches

Differences

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

Link to this comparison view

Next revision
Previous revision
Next revisionBoth sides next revision
rfc:non-capturing_catches [2020/04/05 19:58] – created maxsemrfc:non-capturing_catches [2020/04/06 20:07] – eh maxsem
Line 1: Line 1:
-====== PHP RFC: Your Title Here ======+====== PHP RFC: non-capturing catches ======
   * Version: 0.9   * Version: 0.9
   * Date: 2020-04-05   * Date: 2020-04-05
Line 5: Line 5:
   * Status: Draft   * Status: Draft
   * First Published at: https://wiki.php.net/rfc/non-capturing_catches   * First Published at: https://wiki.php.net/rfc/non-capturing_catches
- 
  
 ===== Introduction ===== ===== Introduction =====
-The elevator pitch for the RFC. The first paragraph of this section will be slightly larger to give it emphasisplease write good introduction.+Currently, PHP requires to capture the exception being caught to a variable: 
 +<code php> 
 +try { 
 +    foo(); 
 +} catch (SomeException $ex) { 
 +    die($ex->getMessage()); 
 +
 +</code> 
 + 
 +However, you need to specify the variable even if it's not used: 
 + 
 +<code php> 
 +try { 
 +    changeImportantData(); 
 +} catch (PermissionException $ex) { 
 +    echo "You don't have permission to do this"; 
 +
 +</code> 
 + 
 +Someone reading the above code is left to wonder if the author intended to not use the exception variable or it'bug.
  
 ===== Proposal ===== ===== Proposal =====
 +Allow catching exceptions without capturing them to variables:
 +<code php>
 +try {
 +    changeImportantData();
 +} catch (PermissionException) { // The intention is clear: exception details are irrelevant
 +    echo "You don't have permission to do this";
 +}
 +</code>
  
 ===== Prior art ===== ===== Prior art =====
 +Such change was first proposed in [[https://wiki.php.net/rfc/anonymous_catch|this RFC]] 7 years ago. It was turned down mostly because it also wanted to allow blanket catches like this:
 +<code php>
 +try {
 +    foo();
 +} catch {
 +    bar();
 +}
 +</code>
 +Meanwhile what I'm proposing here has received mostly positive feedback so I feel it's worth trying.
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
rfc/non-capturing_catches.txt · Last modified: 2020/05/26 14:39 by maxsem