Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented 'finally' keywords for php
RFC: https://wiki.php.net/rfc/finally FR: https://bugs.php.net/bug.php?id=32100 and I have got some improvment ideas(performance), will implemented later. thanks
- Loading branch information
Showing
25 changed files
with
1,830 additions
and
686 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--TEST-- | ||
Try catch finally | ||
--FILE-- | ||
<?php | ||
function foo ($throw = FALSE) { | ||
try { | ||
echo "try\n"; | ||
if ($throw) { | ||
throw new Exception("ex"); | ||
} | ||
} catch (Exception $e) { | ||
echo "catch\n"; | ||
} finally { | ||
echo "finally\n"; | ||
} | ||
|
||
echo "end\n"; | ||
} | ||
|
||
foo(); | ||
echo "\n"; | ||
foo(true); | ||
?> | ||
--EXPECTF-- | ||
try | ||
finally | ||
end | ||
|
||
try | ||
catch | ||
finally | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--TEST-- | ||
Try catch finally return | ||
--FILE-- | ||
<?php | ||
function foo () { | ||
try { | ||
echo "try\n"; | ||
return 1; | ||
} catch (Exception $e) { | ||
} finally { | ||
echo "finally\n"; | ||
} | ||
return 2; | ||
} | ||
|
||
var_dump(foo()); | ||
?> | ||
--EXPECTF-- | ||
try | ||
finally | ||
int(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--TEST-- | ||
Try catch finally multi-return | ||
--FILE-- | ||
<?php | ||
function dummy($msg) { | ||
var_dump($msg); | ||
} | ||
|
||
function foo ($a) { | ||
try { | ||
dummy("try"); | ||
return $a; | ||
} catch (Exception $e) { | ||
throw $e; | ||
} finally { | ||
dummy("finally"); | ||
return "finally"; | ||
} | ||
return "end"; | ||
} | ||
|
||
function &bar($a) { | ||
try { | ||
echo "try\n"; | ||
throw new Exception("ex"); | ||
} catch (Exception $e) { | ||
} finally { | ||
return $a; | ||
} | ||
return ($c = "end"); | ||
} | ||
var_dump(foo("para")); | ||
var_dump(bar("para")); | ||
?> | ||
--EXPECTF-- | ||
string(3) "try" | ||
string(7) "finally" | ||
string(7) "finally" | ||
try | ||
string(4) "para" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
--TEST-- | ||
Nesting try catch finally | ||
--FILE-- | ||
<?php | ||
|
||
function throw_exception($msg) { | ||
throw new Exception($msg); | ||
} | ||
|
||
function foo (&$ex) { | ||
try { | ||
echo "1"; | ||
try { | ||
echo "2"; | ||
throw_exception("try"); | ||
} catch (Exception $e) { | ||
echo "3"; | ||
throw_exception("catch"); | ||
} finally { | ||
echo "4"; | ||
throw_exception("finally"); | ||
} | ||
} catch (Exception $e) { | ||
$ex = $e; | ||
echo "3"; | ||
} finally { | ||
echo "2"; | ||
} | ||
return 1; | ||
} | ||
|
||
var_dump(foo($ex)); | ||
|
||
do { | ||
var_dump($ex->getMessage()); | ||
} while ($ex = $ex->getPrevious()); | ||
?> | ||
--EXPECT-- | ||
123432int(1) | ||
string(7) "finally" | ||
string(5) "catch" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--TEST-- | ||
Try catch finally with return | ||
--FILE-- | ||
<?php | ||
function foo ($a) { | ||
try { | ||
throw new Exception("ex"); | ||
} catch (PdoException $e) { | ||
die("error"); | ||
} catch (Exception $e) { | ||
return 2; | ||
} finally { | ||
return 3; | ||
} | ||
return 1; | ||
} | ||
|
||
var_dump(foo("para")); | ||
?> | ||
--EXPECTF-- | ||
int(3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
--TEST-- | ||
Try catch finally: re-throw exception in catch block | ||
--FILE-- | ||
<?php | ||
function foo ($a) { | ||
try { | ||
throw new Exception("ex"); | ||
} catch (Exception $e) { | ||
var_dump($a); | ||
throw $e; | ||
} finally { | ||
var_dump("finally"); | ||
return "return"; | ||
} | ||
return 1; | ||
} | ||
|
||
try { | ||
var_dump(foo("para")); | ||
} catch (Exception $e) { | ||
"caught exception" . PHP_EOL; | ||
var_dump($e->getMessage()); | ||
} | ||
?> | ||
--EXPECT-- | ||
string(4) "para" | ||
string(7) "finally" | ||
string(2) "ex" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--TEST-- | ||
Try catch finally | ||
--FILE-- | ||
<?php | ||
|
||
class AE extends Exception {}; | ||
class BE extends Exception {}; | ||
|
||
function foo () { | ||
try { | ||
try { | ||
try { | ||
throw new Exception("try"); | ||
} catch (AE $e) { | ||
echo "0"; | ||
die("error"); | ||
} finally { | ||
echo "1"; | ||
} | ||
} finally { | ||
echo "2"; | ||
} | ||
} catch (BE $e) { | ||
die("error"); | ||
} catch (Exception $e) { | ||
echo "3"; | ||
} finally { | ||
echo "4"; | ||
} | ||
return 1; | ||
} | ||
|
||
var_dump(foo()); | ||
?> | ||
--EXPECTF-- | ||
1234int(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
--TEST-- | ||
Try catch finally | ||
--FILE-- | ||
<?php | ||
|
||
class AE extends Exception {}; | ||
class BE extends Exception {}; | ||
|
||
function foo () { | ||
try { | ||
try { | ||
try { | ||
try { | ||
echo "1"; | ||
throw new Exception("try"); | ||
} catch (AE $e) { | ||
die("error"); | ||
} finally { | ||
echo "2"; | ||
} | ||
} finally { | ||
echo "3"; | ||
} | ||
} catch (BE $e) { | ||
die("error"); | ||
} finally { | ||
echo "4"; | ||
} | ||
} catch (Exception $e) { | ||
echo "5"; | ||
} catch (AE $e) { | ||
die("error"); | ||
} finally { | ||
echo "6"; | ||
} | ||
return 7; | ||
} | ||
|
||
var_dump(foo()); | ||
?> | ||
--EXPECTF-- | ||
123456int(7) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
--TEST-- | ||
Try catch finally | ||
--FILE-- | ||
<?php | ||
|
||
class AE extends Exception {}; | ||
class BE extends Exception {}; | ||
|
||
function foo () { | ||
try { | ||
try { | ||
try { | ||
throw new Exception("try"); | ||
} catch (AE $e) { | ||
die("error"); | ||
} finally { | ||
echo "1"; | ||
return 1; | ||
} | ||
} finally { | ||
echo "2"; | ||
return 2; | ||
} | ||
} catch (BE $e) { | ||
die("error"); | ||
} catch (Exception $e) { | ||
echo "3"; | ||
} finally { | ||
echo "4"; | ||
return 4; | ||
} | ||
return 5; | ||
} | ||
|
||
var_dump(foo()); | ||
?> | ||
--EXPECTF-- | ||
1234int(4) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--TEST-- | ||
Try catch finally | ||
--CREDITS-- | ||
adoy | ||
--FILE-- | ||
<?php | ||
function dummy($msg) { | ||
var_dump($msg); | ||
} | ||
try { | ||
try { | ||
var_dump("try"); | ||
return; | ||
} catch (Exception $e) { | ||
dummy("catch"); | ||
throw $e; | ||
} finally { | ||
dummy("finally"); | ||
} | ||
} catch (Exception $e) { | ||
dummy("catch2"); | ||
} finally { | ||
dummy("finally2"); | ||
} | ||
var_dump("end"); | ||
?> | ||
--EXPECTF-- | ||
string(3) "try" | ||
string(7) "finally" | ||
string(8) "finally2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--TEST-- | ||
Try finally | ||
--FILE-- | ||
<?php | ||
function foo ($a) { | ||
try { | ||
throw new Exception("ex"); | ||
} finally { | ||
var_dump($a); | ||
} | ||
} | ||
|
||
foo("finally"); | ||
?> | ||
--EXPECTF-- | ||
string(7) "finally" | ||
|
||
Fatal error: Uncaught exception 'Exception' with message 'ex' %s | ||
Stack trace: | ||
#0 %stry_finally_001.php(%d): foo('finally') | ||
#1 {main} | ||
thrown in %stry_finally_001.php on line %d | ||
|
Oops, something went wrong.