Take the State of PHP 2026 Survey →

support finally keyword View as Markdown

Implemented (PHP 5.5)
Authors
Xinchen Hui laruence@php.net
Date
2012/07/24
Version
1.0
Source
wiki.php.net/rfc/finally

Vote

Should the implementation be merged into trunk? closed
Yes
25
No
5
30 voters
ab Yes 2012-08-06 12:20
aharvey Yes 2012-08-06 13:46
arpad Yes 2012-08-06 14:50
bjori No 2012-08-11 18:19
colder Yes 2012-08-06 11:42
derick No 2012-08-06 11:45
dragoonis Yes 2012-08-06 11:44
felipe Yes 2012-08-06 11:47
googleguy Yes 2012-08-06 15:45
guilhermeblanco Yes 2012-08-10 04:25
hytest Yes 2012-08-08 02:55
indeyets Yes 2012-08-06 13:00
ircmaxell Yes 2012-08-06 15:55
irker Yes 2012-08-09 12:38
joey No 2012-08-07 03:48
jpauli Yes 2012-08-07 08:49
kassner Yes 2012-08-06 11:47
klaussilveira Yes 2012-08-08 06:13
kriscraig Yes 2012-08-06 18:15
laruence Yes 2012-08-07 06:17
levim No 2012-08-06 15:53
nikic No 2012-08-06 11:48
pajoye Yes 2012-08-08 14:18
rasmus Yes 2012-08-08 14:55
sascham78 Yes 2012-08-06 15:16
shm Yes 2012-08-07 07:09
stas Yes 2012-08-06 19:17
tyrael Yes 2012-08-06 12:25
willfitch Yes 2012-08-06 11:47
zhangzhenyu Yes 2012-08-06 13:40

Discussions on the mailing list

[RFC] Supports 'finally' keyword for PHP exceptions
by Laruence · 35 messages
[Voting] Call for voting for RFC: Supports finally keyword
by Laruence · 2 messages

RFC text

Copied from wiki.php.net, last synchronized Oct 13, 2026 11:52 UTC

Introduction

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:

<?php
$db = mysqli_connect();
try {
   call_some_function($db);
} catch (Exception $e) {
   mysqli_close($db);
   throw $e;
}
mysql_close($db);

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

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.

<?php
$db = mysqli_connect();
try {
   call_some_function($db);//the function may throw exceptions which we can not handle
} finally {
   mysqli_close($db);
}

the most confuse part is call “return in try/catch block”, in this case the finally block will still be called

<?php
  try {
    return 2;
  } finally {
    echo "this will be called\n";
  }
  //this will never be called
  echo "you can not see me";

the above script will output:

this will be called
//return int(2)

and for nesting try catch finally:

<?php
function foo ($a) {
   try {
      echo "1";
      try {
        echo "2";
        throw new Exception("ex");
      } catch (Exception $e) {
        echo "3";
      } finally {
        echo "4";
        throw new Exception("ex");
      } 
   } catch (Exception $e) {
      echo "3";
   } finally {
      echo "2";
   }
   return 1;
}
 
var_dump(foo("para"));

will output:

123432int(1)

There are also lots of edge cases could be found in the Test&Examples section blow

Patch

Tests & Examples

Vote

Vote: Should the implementation be merged into trunk? — Yes: 25 · No: 5 (closed)

Changelog

  • 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