A finally block may also be specified after or instead of catch blocks.
Code within the finally block will always be executed after the try and
catch blocks, even if a return or yield statement is encountered,
or if a Throwable error is not caught or is rethrown.
It will be executed prior to resuming the normal execution flow.
The only exception to this rule is when the script is terminated abruptly,
such as by a call to exit() or by a fatal error
(for example, when the maximum execution time is exceeded).
0.2
First finally.
Caught error: Division by zero
Second finally.
Hello World
When the call stack is unwound after a Throwable error
has been thrown, all finally blocks it encounters along the way will be executed.
In other words, nested finally blocks are executed even if the
Throwable error is not caught by an adjacent
catch block.
Interaction between a finally block and a return statement
One notable interaction is between the finally block and a return statement.
If a return statement is encountered inside either the try or the catch block,
the finally block will still be executed. Moreover, the return statement is
evaluated when encountered, but the result will be returned after the finally block
is executed. Additionally, if the finally block also contains a return statement,
the value from the finally block is returned.
Example #3 Interaction between return in finally block and a previous return
Interaction between a finally block and an exception thrown in a try block
If both the try block and the finally block throw an exception,
the exception thrown from the finally block is the one that is propagated,
and the exception thrown from the try block will be used as its previous exception.
Example #5 Exception in try and in finally
<?php
try {
try {
throw new Exception(message: 'Third', previous: new Exception('Fourth'));
} finally {
throw new Exception(message: 'First', previous: new Exception('Second'));
}
} catch (Exception $e) {
var_dump(
$e->getMessage(),
$e->getPrevious()->getMessage(),
$e->getPrevious()->getPrevious()->getMessage(),
$e->getPrevious()->getPrevious()->getPrevious()->getMessage(),
);
}
If, during the traversal of a generator, a Throwable
error is thrown, the finally block within the generator will be executed,
as it is the previous element of the call stack.
Example #6
finally block being run when an Exception
has been thrown
The above example will output
something similar to:
1
finally
outer catch
It is possible to yield within a finally block, but if a
Throwable error occurs and is not caught within
the foreach, a Cannot yield from finally in a force-closed
generatorError is thrown.
The above example will output
something similar to:
1
finally
Fatal error: Uncaught Exception in /in/GbFmu:19
Stack trace:
#0 {main}
Next Error: Cannot yield from finally in a force-closed generator in /in/GbFmu:12
Stack trace:
#0 /in/GbFmu(19): generate()
#1 {main}
thrown in /in/GbFmu on line 12
Interaction between a finally block and a call to exit()
When exit() is called within a try or a catch block,
all finally blocks are skipped.
Example #8 Interaction between a finally block and exit() called in a try block