Skip to content

Instantly share code, notes, and snippets.

@mannion007
Last active October 8, 2016 16:05
Show Gist options
  • Save mannion007/3ddb880d3d2acd938332358bc87f0707 to your computer and use it in GitHub Desktop.
Save mannion007/3ddb880d3d2acd938332358bc87f0707 to your computer and use it in GitHub Desktop.
Surprising behaviour of try/catch/finally block with return statements
<?php
function exceptionThrower()
{
try {
throw new \Exception('Bang!');
} catch(\Exception $e) {
echo 'An exception happened but I caught it!' . PHP_EOL;
return 5;
} finally {
echo 'I am just doing some cleanup...' . PHP_EOL;
return 10;
}
}
echo 'The result is ' . exceptionThrower();
/**
* An exception happened but I caught it!
* I am just doing some cleanup...
* The result is 10
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment