Skip to content

Instantly share code, notes, and snippets.

@fluxsauce
Created July 2, 2013 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fluxsauce/5911294 to your computer and use it in GitHub Desktop.
Save fluxsauce/5911294 to your computer and use it in GitHub Desktop.
PHP 5.5 - try, catch and finally psedocode
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