Get "PHP 8 in a Nuthshell" (Now comes with PHP 8.3)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

When to use finally in exception handling in PHP

Exception handling is an essential part of the software development workflow. It is used to handle the runtime errors so that the normal flow of the application can be maintained.

PHP has a robust mechanism of handling exception using “try-catch” blocks where an exception can be thrown from the “try” block and the same can be caught in the “catch” block. Onwards from PHP 5.5, the language has been introduced with an another block called “finally”. And in this article, I’m going to talk about the same and usecases where you should use it.

The finally block

In PHP 5.5, a “finally” block can be used after or instead of catch blocks. So, essentially the code within the “finally” block will always be executed after the try and catch blocks, regardless of whether an exception has been thrown, and before normal execution resumes.

Here’s an example of how you can write a “try-catch” block:

<?php

class TestException
{
    public function testing()
    {
        try {
            throw new Exception('foo!');
        } catch (Exception $e) {
            return $e->getMessage();
        } 

        echo "executing after try-catch block...\n";
    }
}

$foo = new TestException;
echo $foo->testing();

// foo!

?>

Here, in the above example, we’ve thrown an exception and returned that exception from the catch block. Thing here is, the execution won’t reach “echo” statement after the “try-catch” block as we’re returning the message. You can check this here in this sandbox. That’s where finally comes into the picture.

Using finally block

Adding an additional finally block and moving the “echo” statement into that block in the above example will make sure that the statement will get executed even if we return from the “catch” block. Here’s how you can do it.

<?php

class TestException 
{
    public function testing()
    {
        try {
            throw new Exception('foo!');
        } catch (Exception $e) {
            return $e->getMessage();
        } finally {
            echo "executing in the finally block...\n";    
        }
        
    }
}

$foo = new TestException;
echo $foo->testing();

// executing in the finally block...
// foo!

?>

Here, even if we’ve returned from the “catch” block, the execution won’t stop there. The code inside of the “finally” block will always be executed.

Also note here that, the code in the “finally” block is being printed first and then the statement returned from the “catch” block. Test it here in this sandbox.

When to actually use the finally block

So, the question comes here when you should use the “finally” block?

The answer is, you can use in the scenario where you’d like to do cleanup kind of stuff such as cleaning the cache after file manipulation or in the situations where you’d like to dispatch some events even there’s an exception beforehand.

Laravel use this approach in many of its core classes where it dispatches an event regardless of an exception.

public static function withoutEvents(callable $callback)
{
    $dispatcher = static::getEventDispatcher();

    static::unsetEventDispatcher();

    try {
        return $callback();
    } finally {
        if ($dispatcher) {
            static::setEventDispatcher($dispatcher);
        }
    }
}

If you’ve notice in the example above, you can entirely skip the “catch” block to let the exception itself propagate up the call stack so it can be dealt with separately.

In Closing

I hope you’ve learnt a thing or two in this brief article about exception handling and more specifically about the “finally” block. If you find something wrong in this article, I’d like you address it in the comments.

Until next time!

Learn the fundamentals of PHP 8 (including 8.1, 8.2, and 8.3), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article? Consider leaving a

Tip

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?