rfc:finally

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:finally [2012/07/24 17:09] – [Proposal] laruencerfc:finally [2017/09/22 13:28] (current) – external edit 127.0.0.1
Line 2: Line 2:
   * Version: 1.0   * Version: 1.0
   * Date: 2012/07/24   * Date: 2012/07/24
-  * Author: Xinchen Hui <laruence.php.net> +  * Author: Xinchen Hui <laruence@php.net> 
-  * Status: Under Discussion+  * Status: Implemented
   * First Published at: http://wiki.php.net/rfc/finally   * First Published at: http://wiki.php.net/rfc/finally
 +  * Alternative Reading: <Finally Getting finally In PHP?> http://sheriframadan.com/2012/08/finally-keyword-in-php/  by GoogleGuy
  
 ===== Introduction ===== ===== Introduction =====
 This RFC is try to introduce 'finally' support for exceptions. which is requested from FR #32100, #36779 This RFC is try to introduce 'finally' support for exceptions. which is requested from FR #32100, #36779
  
 +without this feature, user have to write codes like following one to do cleanup while an un-handlable exception occurred:
 +<code php>
 +<?php
 +$db = mysqli_connect();
 +try {
 +   call_some_function($db);
 +} catch (Exception $e) {
 +   mysqli_close($db);
 +   throw $e;
 +}
 +mysql_close($db);
 +</code>
 +
 +introducing finally is not about save one or two lines for such situation, it's about give the user a more proper way to handle such issue.
 ===== Proposal ===== ===== Proposal =====
 The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated. The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.
Line 14: Line 29:
 <code php> <code php>
 <?php <?php
-   try { +$db mysqli_connect(); 
-     $fp fopen("/tmp/1.php", "w"); +try { 
-     //error occurred +   call_some_function($db);//the function may throw exceptions which we can not handle 
-     throw new Exception($e); +} finally { 
-   } finally {  +   mysqli_close($db); 
-     fclose($fp); +}
-   }+
 </code> </code>
  
Line 37: Line 51:
 </code> </code>
  
-the above script will outpu:+the above script will output:
 <code> <code>
 this will be called this will be called
Line 73: Line 87:
 123432int(1) 123432int(1)
 </code> </code>
 +
 + There are also lots of edge cases could be found in the Test&Examples section blow
 ===== Patch ===== ===== Patch =====
   * Patch: https://github.com/laruence/php-src/tree/finally   * Patch: https://github.com/laruence/php-src/tree/finally
-  * Examples: https://github.com/laruence/php-src/commit/c79af6b2a08d98a330dd017436685bcbc4c1a01c+===== Tests & Examples ===== 
 +      * https://github.com/laruence/php-src/blob/finally/Zend/tests/catch_finally_001.phpt 
 +      * https://github.com/laruence/php-src/blob/finally/Zend/tests/catch_finally_002.phpt 
 +      * https://github.com/laruence/php-src/blob/finally/Zend/tests/catch_finally_003.phpt 
 +      * https://github.com/laruence/php-src/blob/finally/Zend/tests/catch_finally_004.phpt 
 +      * https://github.com/laruence/php-src/blob/finally/Zend/tests/catch_finally_005.phpt 
 +      * https://github.com/laruence/php-src/blob/finally/Zend/tests/catch_finally_006.phpt 
 +      * https://github.com/laruence/php-src/blob/finally/Zend/tests/try_finally_001.phpt 
 +      * https://github.com/laruence/php-src/blob/finally/Zend/tests/try_finally_002.phpt 
 +      * https://github.com/laruence/php-src/blob/finally/Zend/tests/try_finally_003.phpt 
 + 
 +===== Vote ===== 
 +<doodle  
 +title="Should the implementation be merged into trunk?" auth="laruence" voteType="single" closed="true"> 
 +   * Yes 
 +   * No 
 +</doodle>
 ===== Changelog ===== ===== Changelog =====
   * 2012/07/24 Xinchen Hui: Initial version   * 2012/07/24 Xinchen Hui: Initial version
 +  * 2012/07/26 Xinchen Hui: Update RFC  
 +  * 2012/08/06 Xinchen Hui: Open voting
 +  * 2012/08/13 Xinchen Hui: Close voting, RFC win the voting
 +  * 2012/08/15 Xinchen Hui: Committed
rfc/finally.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1