Newsgroups: php.internals,php.internals Path: news.php.net Xref: news.php.net php.internals:87451 php.internals:87452 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 90343 invoked from network); 31 Jul 2015 19:44:32 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 31 Jul 2015 19:44:32 -0000 X-Host-Fingerprint: 68.118.157.39 68-118-157-39.dhcp.mdsn.wi.charter.com Received: from [68.118.157.39] ([68.118.157.39:16298] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id B2/00-24609-E10DBB55 for ; Fri, 31 Jul 2015 15:44:30 -0400 To: internals@lists.php.net,Markus Malkusch , internals@lists.php.net Message-ID: <55BBD019.5030106@stephencoakley.com> Date: Fri, 31 Jul 2015 14:44:25 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.8.0 MIME-Version: 1.0 References: In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Posted-By: 68.118.157.39 Subject: Re: Throwable::addSuppressed() From: me@stephencoakley.com (Stephen Coakley) On 07/28/2015 04:51 PM, Markus Malkusch wrote: > Hi PHP > > So I read that there's this Throwable interface coming. Great! How about > extending it with one further method: > > void Throwable::addSuppressed(Throwable exception) > > Semantic is the same as Java's Throwable.addSuppressed()ยน. > > Why? Well consider a code fragment which wants to close a resource > during an exception: > > } catch (Exception $e1) { > try { > $resource->close(); > throw $e1; > > } catch (ResourceException $e2) { > // The information about $e2 is lost. > throw $e1; > } > } > > Currently PHP has no method to propagate both $e1 and $e2. With > Throwable::addSuppressed() $e2 could be added as a suppressed exception > to $e1: > > } catch (Exception $e1) { > try { > $resource->close(); > > } catch (ResourceException $e2) { > e1->addSuppressed($e2); > > } > throw $e1; > } > > To make this information useful (for e.g. a logger) there's one further > method needed: > > Throwable[] Throwable::getSuppressed() > > So PHP, what do you think, might a RFC find acceptance? > > Best wishes > Markus Malkusch > > [1]: > http://docs.oracle.com/javase/8/docs/api/java/lang/Throwable.html#addSuppressed-java.lang.Throwable- > Interesting thought, but how is this different than including a previous throwable and throwing a new, more descriptive exception? } catch (Exception $e1) { try { $resource->close(); } catch (ResourceException $e2) { // Pass in $e2 as the previous exception in the chain. throw new ResourceFailedException($e1->getMessage(), $e1->getCode(), $e2); } } What you're describing doesn't seem like a common use case. Also, this is pretty nitpicky, but the above code might fit better with a finally block when closing resources: try { // Do something with $resource here... } finally { $resource->close(); } -- Stephen Coakley