Edit report at http://bugs.php.net/bug.php?id=32100&edit=1

 ID:               32100
 Comment by:       a dot e at inne dot pl
 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.*

 New Comment:

Could finally also mean that 'returns' will be executed after the
finally block



try{

   some ifs

   ...

   return x

   ...

   more ifs

   ...

   throw

   ... 

   return y

}catch{

   handle exceptions

}finally{

   No matter if there was exception or not

   execute this bit before you leave the method.

   For example if object has some state it might be necessary to 

   make sure its consistent at the end

}



In the case i have now at work i had to add method call before every
return and throw to make sure that my data will be set properly before
method ends.



Would that be a feature someone might like?



thanks



art


Previous Comments:
------------------------------------------------------------------------
[2005-02-25 20:27:50] ceefour at gauldong dot net

I don't think the code is absolutely equivalent. And omitting the
rethrow statement gives up the whole notion of 'finally'.



Actually my code was trying to *emulate* finally. But it's not the right
thing to do. Finally should not even touch the Exception at all...
Finally doesn't even know there is an exception.



I have to agree that 'finally' is not _required_ by PHP, but not by
'we'. 'We' in this sense refers to 'all PHP developers' and that
includes me, and I _need_ (although not _require_) this functionality.
Almost the same as namespaces don't have to be in PHP but some people
feel the need for it. However namespaces are much harder to implement
yet I think finally is relatively straightforward since we can already
emulate it using try/catch, but with the quirks.



I don't think finally is a control flow block. By emulating finally
using try/catch, yes maybe, but we have no other choice. Finally is not
a control flow because why..? Finally has no idea whether it is inside
an Exception or not, and cannot handle it i.e. it's not able to
_control_ processing based on the state of Exception. In this sense
finally is unconditional, just like ordinary statements but they're also
executed when Exception occurs.



IMHO Java has no responsibility here, I think exceptions, try, catch,
finally, are fully the domain of Object Oriented [Design &] Programming.
Delphi, C++Builder, C++, etc. has it, not just Java.



And even if it does, 'design' is out of the scope of PHP. PHP is a
programming language, not a design tool. PHP isn't strictly procedural
and also isn't strictly object-oriented. It's just a matter of taste.
"Be conservative with what you emit and be liberal with what you accept"
and everyone's going to be happy.

------------------------------------------------------------------------
[2005-02-25 19:58:49] he...@php.net

We've had long discussions and came to the only conclusion that we don't
need that, for more search the mailing list archieves.



Besides the following is absolutley equivalent:

mysql_query("LOCK TABLES mytable WRITE");

try {

  // ... do lots of queries here

} catch (Exception $e) {

  // do nothing here

}

 mysql_query("UNLOCK TABLES");



The only difference is the second example does rethrow the exception.
Though this is still possible (however much more to type) it is wrong
design. Since obviously you are using the exceptions as control flow. 



And that design looks like Java where it unlike with PHP makes somewhat
sense.

------------------------------------------------------------------------
[2005-02-25 07:07:44] ceefour at gauldong dot net

Description:
------------
PHP 5 has specifically decided not to support 'finally'. Why?



This is one of numerous cases why finally is useful:



mysql_query("LOCK TABLES mytable WRITE");

try {

  // ... do lots of queries here

} finally {

  mysql_query("UNLOCK TABLES");

}



You need to use UNLOCK TABLES otherwise your tables won't get unlocked
when an exception is thrown. This is especially bad if you use
persistent connections.



It is possible to emulate finally using try/catch but this introduces
code redundancy and may create inconsistent code:



mysql_query("LOCK TABLES mytable WRITE");

try {

  // ... do lots of queries here

  mysql_query("UNLOCK TABLES");

} catch(Exception $e) {

  mysql_query("UNLOCK TABLES");

  throw $e;

}





------------------------------------------------------------------------



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=32100&edit=1

Reply via email to