Created
July 2, 2013 17:29
-
-
Save fluxsauce/5911294 to your computer and use it in GitHub Desktop.
PHP 5.5 - try, catch and finally psedocode
This file contains hidden or 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
lock tables | |
// attempt to write to the database, then unlock the tables. | |
try { | |
writing to db | |
unlock tables | |
} | |
catch exception { | |
If there’s a problem, report the problem and unlock the tables. Notice that I have to manually unlock the tables in both steps. | |
report problem | |
unlock tables | |
} | |
In 5.5, a new block is now available, called “finally”. Finally executes after a try/catch block and before normal execution resumes, which allows cleanup operations to be run even if there was a problem. | |
Here’s how the same functionality would look in PHP 5.5, with less redundant code. | |
lock tables | |
try { | |
writing to db | |
} | |
catch { | |
report problem | |
} | |
finally { | |
unlock tables | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment