This is a quick post on a “simple” thing which took me like like 40mins (too long) to figure out why. All the issue was revolving between how I was using Try-Catch with Finally and a return statement.
Let’s consider this block of code:
NOTE: The code indentation is not rendering great below, so I have also put a copy on a gist – click here!
/** * To demonstrate the effect of a return in a finally * * @author Khayrattee Wasseem <wasseem@khayrattee.com> * @link http://7php.com (blog) * @link http://Khayrattee.com (website) * @copyright 2007-2015 Khayrattee.com */ class ClassA { public function hey() { try { echo ' try from ClassA'; throw new Exception('Exception from ClassA'); } catch (Exception $error) { echo ' catch from ClassA'; throw new Exception($error->getMessage()); } finally { echo ' Finally from ClassA'; } } } class ClassB { public static function hello() { $obj = new ClassA(); try { echo ' try from ClassB'; $obj->hey(); } catch(Exception $error) { echo ' catch from ClassB'; throw new Exception($error->getMessage()); } finally { } return ' finally from ClassB'; } } try { echo ' try from calling file'; ClassB::hello(); } catch (Exception $error){ echo ' catch from calling file with error: ' . $error->getMessage(); } finally { echo ' finally from calling file'; }
OUTPUT: (as you would have guessed is OK)
try from calling file
try from ClassB
try from ClassA
catch from ClassA
Finally from ClassA
catch from ClassB
catch from calling file with error: Exception from ClassA
finally from calling file
NOTE:
We did not see the message “finally from ClassB”, which is as expected.
The Expectation
In the calling block of code, we should get the exception’s message “Exception from ClassA” – which was the case above, all good!
But I was wrongly using the return statement..
But the thing is, I was not initially getting the message “Exception from ClassA” because of me putting the return statement inside the Finally-block. See wrong code example below:
/** * To demonstrate the effect of a return in a finally * * @author Khayrattee Wasseem <wasseem@khayrattee.com> * @link http://7php.com (blog) * @link http://Khayrattee.com (website) * @copyright 2007-2015 Khayrattee.com */ class ClassA { public function hey() { try { echo ' try from ClassA'; throw new Exception('Exception from ClassA'); } catch (Exception $error) { echo ' catch from ClassA'; throw new Exception($error->getMessage()); } finally { echo ' Finally from ClassA'; } } } class ClassB { public static function hello() { $obj = new ClassA(); try { echo ' try from ClassB'; $obj->hey(); } catch(Exception $error) { echo ' catch from ClassB'; throw new Exception($error->getMessage()); } finally { return ' finally from ClassB'; } } } try { echo ' try from calling file'; ClassB::hello(); } catch (Exception $error){ echo ' catch from calling file with error: ' . $error->getMessage(); } finally { echo ' finally from calling file'; }
OUTPUT:
try from calling file
try from ClassB
try from ClassA
catch from ClassA
Finally from ClassA
catch from ClassB
finally from calling file
Conclusion:
Be careful of how I put my return statement, as when wrongly positioned it can halt execution and not behaving as you would normally expect in the outter most Try-Catch-Finally block. (this stupid mistake might have been because of me coding at 4a.m when my focus was going down as I was coding since the previous night – but it was a nice moment to learn something and refresh that mind!)
Over To You
Anything you want to share with me or other readers concerning Try-Catch-Finally – drop a comment below..