Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Returning From A finally Block Is Deprecated

A return statement inside a finally block always wins: it replaces whatever the try/catch block was about to return, and it silently swallows any exception that was propagating out of the try. Until PHP 8.6, this was fully silent, and a pending exception could disappear without a trace. In PHP 8.6, returning from a finally block emits a deprecation notice, while the runtime behavior itself (the return value winning, the exception being discarded) is unchanged.

PHP code

<?php

function getValue(): string {
    try {
        throw new Exception('boom');
    } finally {
        return 'fallback';
    }
}

echo getValue(), "\n";

?>

Before

fallback

After

PHP Deprecated:  Returning from a finally block is deprecated in /codes/returnInFinallyDeprecated.php on line 7

Deprecated: Returning from a finally block is deprecated in /codes/returnInFinallyDeprecated.php on line 7
fallback

PHP version change

This behavior was deprecated in 8.6.

This behavior changed in .

See Also

Error Messages

Extension

© 2023-2026, Damien Seguy - Exakat