SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Catch exception, but maintain execution.

    OK, after this post, I assumed I'd be able to catch a raised exception and continue with application flow.

    Or more specifically, given this code sample, how could I catch a raised exception ( That::doSomething(); ) yet still continue to process '$this' in the foreach loop.

    PHP Code:
    <?php
    try
    {
        foreach(
    $this as $that)
        {
            
    $that->doSomething();
        }
    }
    catch(
    ThatException $exception)
    {
        
    log_error($exception->getMessage());
    }
    ?>
    Is this even possible?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  2. #2
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    foreach($this as $that) {
        try {
            
    $that->doSomething();
        }
        catch(
    ThatException $exception) {
            
    log_error($exception->getMessage());
        }

        
    // process $that


  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Scratch that, I'm an idiot.
    PHP Code:
    $date_parser = new DateParserManager();
    $date_parser->addParser(new DefaultDateParser());
    $date_parser->addParser(new UKDateParser());
    foreach(array(
    '25/12/2009''my sisters birthday''this tuesday''tomorrow''yesterday''1 day after my birthday') as $date)
    {
        try
        {
            echo 
    date('r'$date_parser->parse($date)), '<br />';
        }
        catch(
    Exception $exception)
        {
            echo 
    $exception->getMessage(), '<br />';
        }

    Edit: Thanks Paul, I took an extra moment and figured it out! Sometimes it's just too easy to go off on a tangent and lose a little focus.
    Off Topic:

    Nice to see you posting again.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


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
  •