SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 37 of 37
  1. #26
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use 'finally' for example to close a file connection

    It does have it's uses is what I'm saying I suppose

    Does php5 support finally?
    Nope

  2. #27
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    New Jersey, USA
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ghurtado
    My bad, I totally misunderstood the meaning of "finally". I guess I dont really see the point of using it then, since the code in the "finally" clause gets executed regardless of the results of the "try" block and as such it doesnt seem that different to me than any regular code that were to exist after the try-catch. Does php5 support finally? I couldn't find any information on this.
    The point of finally is that it is executed in BOTH cases:
    PHP Code:
          try {
          
            print &
    quot;This is try code."
            throw new 
    Exception("An exception");
            print &
    quot;This is never executed.";
          
          } catch (
    SomeExceptionsButNotOthers $e) {
            print &
    quot;An Exception occurred that was handled";
          } 
    finally {
            print &
    quot;This appears if an exception occurred, or if one did not.";
          }
          
     print &
    quot;This only runs if the exception is caught and handled. If the exception is not handledthis code is never reached."; 
    If a non-handled exception, "throw new UnhandledException();" was thrown, the catch block wouldn't handle it. But the finally block would still be run.

    =Austin

    PS: It's not in there:

    Quote Originally Posted by Andi
    > I've stated my opinion in the past (search the archives) and I don't think
    > it'd be feasible (nor critical) to implement this...
    See: this thread for painful details.

    There's at least one site cached by Google that makes the claim that finally is supported, but
    the ultimate documentation
    contains no mention of finally.

    The recommended work-around seems to be "catch (Exception $e)" -- using the most generic possible
    catch to serve as a default catcher. This means that you get messy code, of course, or duplicated
    code (even worse IMO).

    Far be it from me to call the Zend guys morons in a public forum, but «quack, quack»

  3. #28
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    New Jersey, USA
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Austin_Hastings
    The point of finally is that it is executed in BOTH cases:
    Here's a way to get the "finally" behavior in PHP5.

    PHP Code:
      function finally_like()
       {
           try 
    // FINALLY
           
    {
             try 
    // CATCH
             
    {
               
    throw_something();
             }
             catch (
    SomeException $se)
             {
               
    handle_some_exception($se);
               if (
    not_completely_handled($se))
             {
                throw 
    $se;
             }
             
    // else fall out and throw non-exception below
             
    }
     
             throw new 
    NotReallyAnException("Execute 'finally' block");
           }
           
    // FINALLY
           
    catch (Exception $e
           {
             
    do_finally_stuff($e);
             if (!
    isa($e"NotReallyAnException"))
             {
                
    //Real exception not handled by catch block above -- pass along.
                
    throw $e;
             }
           }
       } 
    =Austin

  4. #29
    SitePoint Evangelist ghurtado's Avatar
    Join Date
    Sep 2003
    Location
    Wixom, Michigan
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Austin_Hastings
    The point of finally is that it is executed in BOTH cases:
    Which is why I fail to understand what the difference is between
    PHP Code:
    try {
        
    $someObject->method();
    } catch (
    someException $e) {
        echo 
    "Exception caught";
    finally {
        echo 
    "You will always see this";

    and

    PHP Code:
    try {
        
    $someObject->method();
    } catch (
    someException $e) {
        echo 
    "Exception caught";
    }

    echo 
    "You will always see this, even without finally"
    Garcia

  5. #30
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When an exception is caught, isn't the script execution immediately halted? 'Finally' would also btw catch the exception if you decided to throw it, in the example I gave earlier, you'd still want to close the file handling connection?

    I don't know why Zend forgot to add in 'finally' but I suppose they must have had their reasons, much like we do not have namespaces in PHP5 either

  6. #31
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ghurtado
    Which is why I fail to understand what the difference is between
    Take a less simplistic example:

    PHP Code:
    function myFunct() {
        
    $someObject->method();
        echo 
    "You will not see this";
    }
    try {
      
    myFunct();
    } catch (
    someException $e) {
        echo 
    "Exception caught";

    vs

    PHP Code:
    function myFunct() {
      try {
        
    $someObject->method();
      } 
    finally {
        echo 
    "You will always see this";
      }
    }
    try {
      
    myFunct();
    } catch (
    someException $e) {
        echo 
    "Exception caught";

    If $someObject->method(); throws an exception in the first example, there is no echo. In the second, there will be, because it will get run by the finally after (or before?) the exception is handled.

    Douglas
    Hello World

  7. #32
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    New Jersey, USA
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ghurtado
    Which is why I fail to understand what the difference is between
    PHP Code:
    try {
        
    $someObject->method();
    } catch (
    someException $e) {
        echo 
    "Exception caught";
    finally {
        echo 
    "You will always see this";

    and

    PHP Code:
    try {
        
    $someObject->method();
    } catch (
    someException $e) {
        echo 
    "Exception caught";
    }

    echo 
    "You will always see this, even without finally"
    Ghurtado,

    What happens if an exception that is NOT a "someException" is thrown?

    In the first cast, the finally is run as part of "unwinding the stack". In the second case, the exception rockets through the function to a higher level, not stopping for anything.

    =Austin

  8. #33
    If it aint Dutch it aint much Kilroy's Avatar
    Join Date
    Oct 2003
    Location
    The Netherlands
    Posts
    406
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Am I misunderstanding something, or is there not such a thing as finally in PHP?

    EDIT: Question should be, there isn't any finally construct in PHP, right?

  9. #34
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    New Jersey, USA
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kilroy
    Am I misunderstanding something, or is there not such a thing as finally in PHP?

    EDIT: Question should be, there isn't any finally construct in PHP, right?
    There is no finally in PHP. There is active pushback (see my earlier post) to suggestions that it be added. Ergo, there will likely not be a finally in PHP for some time.

    =Austin

  10. #35
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    Germany
    Posts
    550
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Has anyone tried to use to following?
    PHP Code:
    function exception_handler($exception) {
      echo 
    "Uncaught exception: " $exception"\n";
    }

    set_exception_handler('exception_handler');

    throw new 
    Exception('Uncaught Exception');
    echo 
    "Not Executed\n"
    This would be a nice way to catch any exceptions that are not handled, and in the function you could instantiate a new PageElement/View and then show it.

    When i try to use this code my apache crashes with a 404 Error, can anyone confirm this?

  11. #36
    If it aint Dutch it aint much Kilroy's Avatar
    Join Date
    Oct 2003
    Location
    The Netherlands
    Posts
    406
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That code outputs:

    Uncaught exception: Uncaught Exception
    So it works fine on my installation... running PHP 5.0.1...

    (I know it's old but I'm using an auto-installer so can't upgrade manually)

    I think that might be a cool way to have another level of exception handling, just in case something weird happens.

  12. #37
    SitePoint Evangelist ghurtado's Avatar
    Join Date
    Sep 2003
    Location
    Wixom, Michigan
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your clarification regarding "finally".
    Garcia


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •