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

 ID:                 32100
 Comment by:         pieceofchum at yahoo 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
 Private report:     N

 New Comment:

Hello I am a Java developer and would like to move over to PHP for my current 
personal projects. The use of finally in Java is extremely powerful as it 
ensures that a unit of work that uses any resources that need to be managed are 
guaranteed to be handled before leaving the method even whent here is a catch 
clause. This has nothing to do with control flow and exception handling it has 
everything to do with contract based blocks of code in fact finally is a 
totally unique construct which greatly simplifies algorithms where one needs a 
guarantee of certain code running (usually to handle external resources) no 
matter what happens outside of course of an error (error defined as something 
that breaks the interpreter/compiler/environmen). It is not a mistake of design 
but a vast improvement in code clarity and application of the DRY principle 
which is correct programming and has nothing at all to do with improper control 
flow. It is not a mistake that it is in some form in Python, Ruby, Java etc... 
Please please recondsider adding this extremely important construct to PHP. 

Thanks for your consideration in this matter


Previous Comments:
------------------------------------------------------------------------
[2012-07-05 20:17:57] angelo at camargo dot ws

++ for finally in PHP

------------------------------------------------------------------------
[2012-06-07 09:16:55] jl235 at kent dot ac dot uk

Most of the exceptions people come across in their PHP code tends to be for 
either File IO, or database access. Both of these need a finally to ensure the 
handle/connection/whatever gets closed, or dealt with in some other way. Using 
try/catch is already a lot more cumbersome then a world without Exceptions, but 
without finally, it adds a lot duplication and state management.

For example in my own code I do something along the lines of ...

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

$time = microtime( true );
$sql = generateSQLQuery();
$conn = openDBConnection();
$ex = null;

try {
    $result = runSQLQuery( $conn, $sql );
} catch ( Exception $ex ) { /* do nothing */ }

closeDBConnection( $conn );
logSQLQuery( $sql, microtime(true) - $time );

if ( $ex !== null ) {
    throw $ex;
}

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

... which could just be ...

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

$time = microtime( true );
$sql  = generateSQLQuery();
$conn = openDBConnection();

try {
    $result = runSQLQuery( $conn, $sql );
} finally {
    closeDBConnection( $conn );
    logSQLQuery( $sql, microtime(true) - $time );
}

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

Simpler to write, easier to read, harder to get wrong, and more elegant.

Please re-open this.

------------------------------------------------------------------------
[2012-06-05 11:19:41] sgnezdov at fastmail dot fm

Finally is absolutely necessary for proper management of disposable resources.

There is no easy to read workaround for

try { 
 causeException();
} finally {
 releaseResource();
}

others pointed out that solving this issue kills re-throw, which is equally 
important.

------------------------------------------------------------------------
[2012-05-29 21:36:49] kavi at postpro dot net

Since every other kitchen sink on the planet has been thrown into PHP, why not 
also the refrigerator which we all expect to be here?  Come on.  At least give 
a 
good reason.

Quoting topaz:

"Ugly workaround hack time!

(This is not a substitute for a real language feature!)"


...yeah, you're actually describing the *entire language* there. :|

------------------------------------------------------------------------
[2012-04-25 20:32:33] toplegocreator at hotmail dot com

"Though this is still possible (however much more to type) it is wrong design. 
Since obviously you are using the exceptions as control flow."

If exceptions should never be propagated up the stack to a block of code that 
knows how to deal with them, why are exceptions there in the first place? 
Exceptions ARE flow control. That's their reason for existing. If they 
shouldn't be used that way, they shouldn't be included in the language.

Let me elaborate. An exception should occur any time when the current block 
cannot successfully continue execution in the current state and has no direct 
means (or shouldn't have any direct means because of good separation of 
concerns) of informing the client of the problem. A finally clause, while not 
strictly needed if code repetition is acceptable (and it isn't in my book), is 
appropriate for ensuring that resources (like database connections, open files, 
a printer, whatever) are released when fatal errors occur. Trying to deal with 
the error as some kind of returned result all the way up the stack will just 
make your code more difficult to read and maintain; an error should go all the 
way up the stack until a piece of code that's responsible for output can 
determine how to inform the client. That's how exceptions are supposed to be 
used, and a finally clause makes it possible to properly and (fairly) reliably 
release resources, which is also a best practice.

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


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

    https://bugs.php.net/bug.php?id=32100


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

Reply via email to