Wrongly Positioning a ‘Return’ Statement Inside An Inner ‘Finally’ Block Can Be Lethal 

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!

[lang=php]
/**
* 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’;
}
[/lang]

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:

 

[lang=php]
/**
* 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’;
}
[/lang]

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..

Khayrattee Wasseem's Picture

About Khayrattee Wasseem

PHP enthusiast | The PHP Community Fanboi | Founder of PHP Usergroup for Mauritius (#PhpMauritius) | Aspiring Entrepreneur | Ex-country Manager of a global award winning Magento Agency | Passionate about all things Wordpress & Servers

Mauritius https://wppp.io

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.