Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:67177 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 35908 invoked from network); 26 Apr 2013 19:52:39 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 26 Apr 2013 19:52:39 -0000 Authentication-Results: pb1.pair.com header.from=amaury.bouchard@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=amaury.bouchard@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.216.181 as permitted sender) X-PHP-List-Original-Sender: amaury.bouchard@gmail.com X-Host-Fingerprint: 209.85.216.181 mail-qc0-f181.google.com Received: from [209.85.216.181] ([209.85.216.181:42446] helo=mail-qc0-f181.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 20/F8-15719-60BDA715 for ; Fri, 26 Apr 2013 15:52:39 -0400 Received: by mail-qc0-f181.google.com with SMTP id a22so2301381qcs.12 for ; Fri, 26 Apr 2013 12:52:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:sender:in-reply-to:references:from:date :x-google-sender-auth:message-id:subject:to:cc:content-type; bh=OZ/jTIBUHQhQCFOsn3FJpoZBJ0foUBsDMNrNjmo87HM=; b=ddFxVyu8afHeyDkh8YmgMUpOJzuZKXJn7SiX9mvTuFQlbFZUJsVlD0sXTS9i3eLxeH 1TyEHHAHajyjVPYNkgQOxsEs+I/2in+Xz/vTTp8AtT4kNTv/JO1nF3+GmsG/1VcjTSHj C4BF86t+KeZpW2mP92WlBl50+aikdECkYf4dt3TfPhvhjb6hfB+btJ0jBHvUWFM1kryg W1JqDO8dvWrkeXiBSgKSAbLG+y2JtJvl80SlRsEAJ4t2uLcuk/gPyik7LRlP0Pc1GHlY P5VryxO7k812U17GsAh6eiB8RvsUxkuF8YzM3JAiYTi32GyVbQ/xQ4xqd6/c52tOFbir wKLQ== X-Received: by 10.229.63.2 with SMTP id z2mr422732qch.53.1367005956388; Fri, 26 Apr 2013 12:52:36 -0700 (PDT) MIME-Version: 1.0 Sender: amaury.bouchard@gmail.com Received: by 10.49.133.39 with HTTP; Fri, 26 Apr 2013 12:52:16 -0700 (PDT) In-Reply-To: <517AD23A.7010505@heigl.org> References: <517AD23A.7010505@heigl.org> Date: Fri, 26 Apr 2013 21:52:16 +0200 X-Google-Sender-Auth: 2wwcwJ3nWudLFL8vbCpQ9JhCZCw Message-ID: To: Andreas Heigl Cc: Julien Pauli , PHP Internals Content-Type: multipart/alternative; boundary=001a11c24ed46fdee004db48e045 Subject: Re: [PHP-DEV] Continued try blocks From: amaury@amaury.net (Amaury Bouchard) --001a11c24ed46fdee004db48e045 Content-Type: text/plain; charset=ISO-8859-1 2013/4/26 Andreas Heigl > try { > $foo = $bar->getObject(); > $foo->doSomething() > } catch(Exception $e) { > continue // Or whatever shall be used > } > > When $bar->getObject throws an Exception no $foo is set. So the next > line will result in a "PHP Fatal error: Call to a member function > doSomething() on a non-object". > That's fine to me. It's a software engineering problem you can solve easily: try { $foo = $bar->getObject(); $foo->doSomething(); } catch (ObjectCreationException $oce) { add_to_log('Unable to create object'); throw $oce; // or do something else, // or just do nothing to exit from the try/catch block } catch (Exception $e) { add_to_log($e->getMessage()); continue; } The getObject() method should raise an ObjectCreationException, while the doSomething() method could raise any other type of exception. It's just a natural and smart way to use exceptions. It could be solve differently: try { $foo = $bar->getObject(); $foo->doSomething(); } catch (NotImportantException $nie) { add_to_log($nie->getMessage()); continue; } catch (Exception $e) { add_to_log($e->getMessage()); throw $e; // or do something else, // or just do nothing to exit from the try/catch block } If an expression raises a NotImportantException, it will not interrupt the execution flow. --001a11c24ed46fdee004db48e045--