Re: try/catch/FINALLY

From: Date: Wed, 06 Aug 2003 07:51:11 +0000
Subject: Re: try/catch/FINALLY
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-3756@lists.php.net to get a copy of this message
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

« previous php.internals (#3756) next »