Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:91605 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 49559 invoked from network); 10 Mar 2016 07:31:08 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 10 Mar 2016 07:31:08 -0000 Authentication-Results: pb1.pair.com header.from=bjorn.x.larsson@telia.com; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=bjorn.x.larsson@telia.com; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain telia.com from 81.236.60.154 cause and error) X-PHP-List-Original-Sender: bjorn.x.larsson@telia.com X-Host-Fingerprint: 81.236.60.154 v-smtpout1.han.skanova.net Received: from [81.236.60.154] ([81.236.60.154:43301] helo=v-smtpout1.han.skanova.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 6B/1F-53667-9B221E65 for ; Thu, 10 Mar 2016 02:31:07 -0500 Received: from [192.168.7.7] ([195.198.188.252]) by cmsmtp with SMTP id dv3laNlWFC0O7dv3lauBiJ; Thu, 10 Mar 2016 08:31:01 +0100 To: =?UTF-8?B?QnJvbmlzxYJhdyBCaWHFgmVr?= , Pierrick Charron References: <56DF5B04.1080505@telia.com> Cc: PHP internals Message-ID: <56E122B7.5080407@telia.com> Date: Thu, 10 Mar 2016 08:31:03 +0100 User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-CMAE-Envelope: MS4wfChCrbQlFw+7hbSv2Zt0x/EogGi+qoKgrSErOahBgKxMVPE4cU1WVPFXIalxKdhakiLb9HNNX2qeNL40Li3lvUJ5RNSBNKgLeR8vz2ryQiy7iO21QPB0 OTX/BblSj9a3ur4mSBE+PNK9iibx6lXrMxEDNLdbneXyn34ca6v2U70bIX75kAQuDO+emf0w23/Mgwroky4VTz/J0K1z24QWPoCpNsW5KyYClg0AYXZMGv5B 39PkYeitu/PNA/7HNJX4gg== Subject: Re: [PHP-DEV] [RFC Discussion] Catching multiple exception types From: bjorn.x.larsson@telia.com (=?UTF-8?Q?Bj=c3=b6rn_Larsson?=) Hi, Thanks for clarifying. Think the RFC would benefit from having some of these examples in it. I also had in mind that it's handy for catching many exceptions when logging stuff and re-throwing like: } catch (FirstException | SecondException ex) { logger.error(ex); throw ex; } Regards //Björn PS Sorry for top-posting... Den 2016-03-09 kl. 08:05, skrev Bronisław Białek: > Hi :) > > I think it is feature that is useful time to time, not very often. > One example: > > class PackingFailed extends \Exception implements PackagingException {} > > // this exception could originate from package with assertions/exceptions, > // so I cannot use common interface with above > class EmptyName extends \Exception implements ValidationException {} > > class Packer > { > /** @throws PackingFailed */ > public function pack(PackTemplate $packTemplate) : Pack > { > try { > return $this->save($packTemplate); > } catch (IOException $e) { > throw new PackingFailed('', 0, $e); > } > } > } > > class PackTemplate > { > /** @throws EmptyName */ > public function __construct(string $name) > { > if ($name === '') { > throw new EmptyName(); > } > } > } > > /** @throws SomeException */ > function packeForMe(string $name) : Pack > { > try { > return (new Packer())->pack(new PackTemplate($name)); > } catch (PackingFailed | ValidationException $e) { > throw new SomeException($e); // or return null in other cases > } > } > > > 2016-03-09 2:47 GMT+01:00 Pierrick Charron : > >> Hi Björn, >> >> The only time I had to do this with core PHP exceptions is to make the >> code compatible for both PHP5 and PHP7: >> >> try { >> } catch(\Exceptions $e) { >> } catch(\Throwable $e) { >> } >> >> But it will of course not be applicable since this feature is targeting >> PHP7.1. Other than that the PHP core exception hierarchy is well enough for >> MY needs. But if someone already had to do this fill free to provide your >> use case as an example. >> >> My main target is custom exceptions (even if the logic is applicable on >> everything Throwable). A custom exception use case would be some method >> that throw thwo different kind of exceptions like for example the doctrine >> AbstractQuery::getSingleResult (NoResultException, >> NonUniqueResultException) that you could want to handle the same way. >> >> An other really easy example would be simple code like this one that I >> found in symfony (not really a big deal but still) >> >> } catch (AccessException $e) { >> return false; >> } catch (UnexpectedTypeException $e) { >> return false; >> } >> >> And other piece of code using multiple libraries. >> >> >> On 8 March 2016 at 18:06, Björn Larsson wrote: >> >>> Den 2016-03-08 kl. 22:42, skrev Pierrick Charron: >>> >>>> Hi internals, >>>> >>>> Bronisław Białek and I would like to start a discussion about allowing >>>> multiple exception types to be caught in a single catch statement. >>>> >>>> https://wiki.php.net/rfc/multiple-catch >>>> >>>> A working implementation and tests are available in the RFC. >>>> >>>> We are waiting for your constructive feedback and thoughts. >>>> >>>> Thanks >>>> Pierrick >>>> >>>> Nice RFC! Think it would be good if you had an example in the >>> RFC showing the applicability of catching two php exceptions. >>> Especially given the new exception hierarchy in PHP 7. I'm also >>> pondering if the main target for this is custom exceptions or >>> the built-in ones or both? >>> >>> Regards //Björn Larsson >>> >>> PS >>> >> >