try/catch/FINALLY

php.internals

Cristiano Duarte

23 years ago
Hi all, I know there was a lot of discussion about try/catch, but I will bring it up again: What about finally ? I know someone posted that finally isn't necessary since PHP destroy all variables and releases all resources at the end of the script. But if the script is a PHP-GTK application or a standalone server aplication (like CORBA)? Finally is very useful in situations where you must be certain that some piece of code will be executed no matter what happens (a normal execution, a return statement or an exception). IMHO I think that an implementation start would be hooking the function exit at the engine.. Cristiano Duarte

Cristiano Duarte

23 years ago
Please, ignore this:
> IMHO I think that an implementation start would be hooking the function
exit
> at the engine..
And read this instead: IMHO I think that an implementation start would be hooking the exit of the try statement even if its normal or has an exception thrown... Best regards, Cristiano Duarte "Cristiano Duarte" <cunha17@uol.com.br> escreveu na mensagem news:20030806001540.7978.qmail@pb1.pair.com...
> Hi all, > > I know there was a lot of discussion about try/catch, but I will bring it
up
> again: > > What about finally ? > > I know someone posted that finally isn't necessary since PHP destroy all > variables and releases all resources at the end of the script. But if the > script is a PHP-GTK application or a standalone server aplication (like > CORBA)? > > Finally is very useful in situations where you must be certain that some > piece of code will be executed no matter what happens (a normal execution,
a
> return statement or an exception). > > IMHO I think that an implementation start would be hooking the function
exit

(Marcus Börger)

23 years ago
Hello Cristiano, Wednesday, August 6, 2003, 2:15:39 AM, you wrote: CD> Hi all, CD> I know there was a lot of discussion about try/catch, but I will bring it up CD> again: CD> What about finally ? There's absolute no need for finally: try { } catch (...) { } // here's you're finally code
-- Best regards, Marcus mailto:helly@php.net

Timm Friebe

23 years ago
On Wed, 2003-08-06 at 09:14, Marcus Börger wrote:
> Hello Cristiano,
[...]
> There's absolute no need for finally: > > try { > } > catch (...) { > } > // here's you're finally code
Well, consider: function foo() { try { // ... } catch (Exception $e) { // ...handle it... return false; } finally { // ...clean up... } // ...continue doing whatever... return true; } The cleanup code is always to be executed, the method should of course not continue in case an exception was caught. OK, I could write: function foo() { try { // ... } catch (Exception $e) { // ...handle it... } // ...clean up ... if ($e) return false; // ...continue doing whatever... return true; } so all it boils down to is, I guess, syntactic sugar. Evil programming language sourcecode below:) ---------------------------------------------------------------------------- thekid@thekid:~ > cat FinallyTest.java class FinallyTest { public static void main(String[] args) { boolean success= FinallyTest.test(args.length > 0); System.out.println("Success? " + success); } public static boolean test(boolean dothrow) { try { if (dothrow) { throw new Exception("Test"); } } catch (Exception e) { e.printStackTrace(); return false; } finally { System.out.println("<<< In finally >>>"); } System.out.println("OK"); return true; } } thekid@thekid:~ > CLASSPATH="." java FinallyTest <<< In finally >>> OK Success? true thekid@thekid:~ > CLASSPATH="." java FinallyTest 1 java.lang.Exception: Test at FinallyTest.test(FinallyTest.java:10) at FinallyTest.main(FinallyTest.java:3) <<< In finally >>> Success? false - Timm

Simeon Koptelov

23 years ago
Hello Marcus, Wednesday, August 6, 2003, 1:14:10 PM, you wrote: CD>> What about finally ? MB> There's absolute no need for finally: MB> try { MB> } MB> catch (...) { MB> } MB> // here's you're finally code try { $this->allocateSomeResources(); throw new SomeException(); } catch( AnotherException $e ) { } finally { $this->freeSomeResource(); } This code will call freeSomeResource() -- that's why one may want to have finally. But I agree that there's no need for finally. If uncaught exception is thrown -- it's bad application design or malicious component( you cannot declare throws in function definition so some evil component can do something like " throw new ExceptionThatNobodyHandles(); // har-har-har!" . You need to refactor in first case and can do nothing in second -- if you'll start to free resources in finally after each try-catch or always catch Exception, your code will become garbage soon IMHO.
-- Best regards, Simeon mailto:xi@newmail.ru

Wez Furlong

23 years ago
The most important thing missing from this is that you cannot rethrow the exception from the point where you suggest to place the finally code :-) --Wez.

Jeff Moore

23 years ago
On Wednesday, August 6, 2003, at 03:14 AM, Marcus Börger wrote:
> There's absolute no need for finally:
PHP doesn't necessarily control every resources you might want to deallocate. mysql_query('LOCK ...'); try { ... do stuff } finally { mysql_query('UNLOCK...'); } .. do more stuff

Simeon Koptelov

23 years ago
Hello Cristiano, Wednesday, August 6, 2003, 6:15:39 AM, you wrote: It would be much better if user will have to put method's throws in method's declaration imho( see my reply to Marcus ).
-- Best regards, Simeon mailto:xi@newmail.ru