Edit report at http://bugs.php.net/bug.php?id=32100&edit=1
ID: 32100
Comment by: 128bitencrypted at gmail dot com
Reported by: ceefour at gauldong dot net
Summary: Request 'finally' support for exceptions
Status: Closed
Type: Feature/Change Request
Package: Feature/Change Request
Operating System: *
PHP Version: 5.*
Block user comment: N
New Comment:
I noticed this bug because I have exactly the same problem atm and
finally could solve it. I found a great discussion about the "return"
problem and how it's solved in java. (IMHO I think that's the right
way)
stackoverflow dot com/questions/65035/in-java-does-return-trump-finally
In PHP it could be implemented like the following:
function example1()
{
try {
return "Return\n";
} finally {
echo "Finally\n";
}
}
echo example1();
Output:
Finally
Return
And it's important that finally has the right to overwrite return.
(Although I think that there only a few cases where that would be
useful)
function example2()
{
try {
return "Return wins\n";
} finally {
return "Finally wins\n";
}
}
echo example2();
Output:
Finally wins
I hope that helps a bit more why finally would be a very useful in php!
Thanks.
Previous Comments:
------------------------------------------------------------------------
[2010-09-09 02:39:42] phplasma at gmail dot com
A finally block would indeed be useful, please reconsider.
------------------------------------------------------------------------
[2010-09-07 17:38:51] michael202 at gmx dot de
I also think that try-finally is useful.
try catch is no elegant replacement.
Just needed it today again.
finally is more elegant than an other solution.
For example:
try {
lots o' code
if a return
..
if b exit
..
} finally
do somethin
}
Another solution to the "finally" problem is to use goto.
Not so elegant but not a bad thing according to Knuth.
------------------------------------------------------------------------
[2010-08-16 12:42:11] torsten dot landmann at bauermedia dot com
I also agree: 'finally' is needed. I really don't get why it has been
left out.
There is no elegant equivalent, especially so since rethrowing the
exception alters file and line number saved in the exception, so later
it's hard to find out where it originally came from.
Please offer "finally". Don't worry, nobody will be forced to use it.
I definitely will.
thuejk showed very well how 'finally' helps with keeping your code
clean. Or vice versa: How the absence of it often causes the need to
copy and paste code (which is always a bad development pattern).
------------------------------------------------------------------------
[2010-06-27 00:59:16] thuejk at gmail dot com
>We've had long discussions and came to the only conclusion that we
don't need
that, for more search the mailing list archieves.
Where is that discussion? I haven't been able to find it. Only people
saying
that finally is utterly useless, without showing any signs that they
have
actually considered finally's uses.
As the other comments have said, sometimes some code inside a try will
allocate
a non-php ressource which need to be deallocated whether or not an
exception is
thrown. To avoid writing that code twice, you need it in finally.
Version without finally:
try {
allocate non-php resource
} catch ($ex) {
deallocate non-php resource
throw $ex;
}
deallocate non-php resource
Version with finally:
try {
allocate non-php resource
} finally {
deallocate non-php resource
}
The finally code is obviously "better". And it is a completely
reasonable way to
code.
Sure you can emulate finally with more code, but so can a Turin Machine.
finally
is syntactic sugar which makes it easier to write maintainable programs.
------------------------------------------------------------------------
[2010-06-16 20:54:10] orlandu96 at gmail dot com
are there any updates on this issue?
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
http://bugs.php.net/bug.php?id=32100
--
Edit this bug report at http://bugs.php.net/bug.php?id=32100&edit=1