Finally Catches Exception

<?php

function foo() {
    try {
        throw new Exception('yeah');
    } finally {
        return 1;
    }
}

print foo();
// print 1, no exception

?>

In this code, finally is executed after the throw. Since it contains a return, the function is finalized before the throw is executed. The exception is then lost, and the scripts displays 1. This is why it is recommended to avoid return in a finally clause.

See Also

PHP Features

Last updated: 14 July 2026