Essentially it is to ensure that the flow of that block always runs. Even if an exception occurs in the block started by try
, the block of finally
will run before exiting the function and start to knock down the function call stack.
In general it is used to terminate allocated resources, for example close a file.
There may arise a doubt as to what is the difference to the catch
which is also executed when there is an exception. The difference is that the catch
is only executed when there is an exception. The finally
is executed with or without exception. The execution flow of the program goes from try
to the finally
always, be why the try
closed normally, either because it generated an exception. The catch
is a conditional execution block (by launching an exception), the finally
is enforceable.
Remembering that the release of an exception will force the execution of both the catch
, how much of finally
, but any code that comes after this whole block try-catch-finally
nothing will be executed.
Perhaps you give an impression of little use of it because of examples that do nothing useful effectively.
Documentation.
Good generic example:
$recurso = abrir_recurso();
try {
$resultado = use_rercurso($rercurso);
} finally {
libere_rercurso($rercurso); //acontecendo ou não uma exceção, o recurso será fechado
}
return $resultado;
Actual example taken from that blog:
function addRecord($record, $db) {
try {
$db->lock($record->table);
$db->prepare($record->sql);
$db->exec($record->params);
} catch(Exception $e) {
$db->rollback($record);
if (!write_log($e->getMessage(), $e->getTraceAsString())) {
throw new Exception('Unable to write to error log.');
}
} finally {
$db->unlock($record->table);
}
return true;
}
I put in the Github for future reference.
The example is bad because it captures Exception
and launches again but the general idea is this.
Reading suggestion. It’s not the same language but it works the same way.
+1. makes sense. That’s why I had seen an example dealing with a writing of a
log
, where Finally finishes the file opening– Wallace Maxters
"perhaps it gives a bad impression of him because of examples that do nothing useful" reminds me the answer you gave when I asked the
Array Dereferencing
. The PHP Handbook had to show in a way where it could be used for refactoring (or a simplification, perhaps)– Wallace Maxters
@Wallacemaxters the PHP manual is very bad. And the Portuguese version is awful.
– Maniero
in my tests, after the
finally
, the code was executed normally. That’s what I understood? " but any code that comes after all that Try-catch-Finally block nothing will run" ?– Wallace Maxters
See if the emphasis helps you understand better. In your case nay there has been release of an exception, so the rest of the code will run normally.
– Maniero
@Wallacemaxters Important detail is that the
finally
is executed at all times. Even if you give onereturn
inside the blockcatch
Return will be executed after the code infinally
.– André Ribeiro
"In general it is used to terminate allocated resources, for example close a file."+ 1
– Augusto Vasques