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!

<!--?php &lt;br ?--> /**
* To demonstrate the effect of a return in a finally
*
* @author Khayrattee Wasseem &lt;wasseem@khayrattee.com&gt;
* @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('<strong>Exception from ClassA</strong>');
} catch (Exception $error) {
echo '
catch from ClassA';
throw new Exception($error-&gt;getMessage());
} finally {
echo '
Finally from ClassA';
}
}
}

class ClassB
{
public static function hello()
{
$obj = new ClassA();
try {
echo '
try from ClassB';
$obj-&gt;hey();
} catch(Exception $error) {
echo '
catch from ClassB';
throw new Exception($error-&gt;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-&gt;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:

 

<!--?php &lt;br ?--> /**
* To demonstrate the effect of a return in a finally
*
* @author Khayrattee Wasseem &lt;wasseem@khayrattee.com&gt;
* @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('<strong>Exception from ClassA</strong>');
} catch (Exception $error) {
echo '
catch from ClassA';
throw new Exception($error-&gt;getMessage());
} finally {
echo '
Finally from ClassA';
}
}
}

class ClassB
{
public static function hello()
{
$obj = new ClassA();
try {
echo '
try from ClassB';
$obj-&gt;hey();
} catch(Exception $error) {
echo '
catch from ClassB';
throw new Exception($error-&gt;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-&gt;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..

If You Appreciate What I Do Here On Seven PHP :: 7PHP, You Could Consider:

  1. Following me on Twitter | @7php
  2. LIKE-ing my FaceBook page
  3. Subscribe to my Email List - see top-right subscription box
  4. Help diffuse this interview to the PHP ecosystem - Share & Spread the word as far as you can ==> That would be a FREE way to thank me
  5. Use my DigitalOcean referral link if you plan to use it as your VPS (it starts at only $5/month btw) or if you can refer it to your friends, highly appreciated.
  6. Support via Paypal donate - my Paypal ID is w@7php.com

{I'm thankful to your response(s)!}

Speak Your Mind

*