Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:92582 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 23354 invoked from network); 20 Apr 2016 22:16:25 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 20 Apr 2016 22:16:25 -0000 Authentication-Results: pb1.pair.com header.from=patrickallaert@php.net; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=patrick.allaert@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.48 as permitted sender) X-PHP-List-Original-Sender: patrick.allaert@gmail.com X-Host-Fingerprint: 74.125.82.48 mail-wm0-f48.google.com Received: from [74.125.82.48] ([74.125.82.48:37761] helo=mail-wm0-f48.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id DD/B9-14036-8BFF7175 for ; Wed, 20 Apr 2016 18:16:25 -0400 Received: by mail-wm0-f48.google.com with SMTP id n3so104053728wmn.0 for ; Wed, 20 Apr 2016 15:16:24 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=nj8c4y2YsWVjRPpjfm/SbTVhfhV5yj9lFI9AtWVezeo=; b=eT4VyVquZT3lsldcmB7XlH7q0B0HBAAWfLt8WIuPJAKJZftRGofM0+ZdCy3Wg+PGxm JqwC6xHuGVR/YsdLzBKWrz+PvvrSt4nv2vUgA/6gmRbJwn73ThUi0T2v6CLFLNgRTNIc dS1gEfAyeEI6jWAkH/ieCsNrxOXIvVvQDAq4ekYUQC1SlXBD6rw/6E6doYrBumBSPZdz JABZjF6rpMjs0ySHRjda6HikoE+nAJgRtc2MkmncdiNCEH9BRNS5VjaGiX3H7slGdO0s CG05t/I9pzXUhb2QrdmcySId8SgH1DQHTieKD2FzhMWuRUCQ7TziIjTv56xQfhcxOCda +Ddg== X-Gm-Message-State: AOPr4FU5km//SPqXT6TU2z8Fc/nbOpSKcqBOysUhUlXlyw6/brTxV3OULP6yhi10XmhN1zve2qYTvRKSZIzMMw== X-Received: by 10.28.26.138 with SMTP id a132mr31271718wma.20.1461190581492; Wed, 20 Apr 2016 15:16:21 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 20 Apr 2016 22:16:11 +0000 Message-ID: To: Zeev Suraski , Derick Rethans , Marco Pivetta Cc: PHP internals Content-Type: multipart/alternative; boundary=001a114cbc648f1fee0530f1f11a Subject: Re: [PHP-DEV] [VOTE] Catching Multiple Exception Types From: patrickallaert@php.net (Patrick ALLAERT) --001a114cbc648f1fee0530f1f11a Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Zeev, Le mar. 19 avr. 2016 =C3=A0 11:56, Zeev Suraski a =C3=A9cri= t : > In Drupal 8, many catch blocks simply resort to return FALSE or TRUE. Fo= r > example: > > try { > return $router->match($path); > } > catch (ResourceNotFoundException $e) { > return FALSE; > } > catch (ParamNotConvertedException $e) { > return FALSE; > } > catch (AccessDeniedHttpException $e) { > return FALSE; > } > catch (MethodNotAllowedException $e) { > return FALSE; > } > } > This looks much more like Exceptions used for not "Exceptional" cases: where Exceptions are preferred over a clean API and avoiding at all price if-conditions. One of the very reason I voted "no". It encourages people to replace any kind of condition with an exception, leading to less readable code and incomplete API. > try { > return $router->match($path); > } > catch > (ResourceNotFoundException|ParamNotConvertedException|AccessDeniedHttpExc= eption|MethodNotAllowedException > $e) { > return FALSE; > } > The "return FALSE" here on all those cases is just another way to ease silencing Exceptions which is often a bad idea (like @). It's also perfectly possible to make all those Exceptions implement a "MatchingFailedException" interface (even empty). This would even make the collection reusable AND extensible because new user Exception classes could implement that "MatchingFailedException" interface without the need to refactor code. Which is much, much nicer and readable. And I found this in the ZF2 > codebase: > > } catch (DiRuntimeException $e) { > if ($methodRequirementType & self::RESOLVE_STRICT) { > //finally ( be aware to do at the end of flow) > array_pop($this->currentDependencies); > if (isset($alias)) { > array_pop($this->currentAliasDependenencies)= ; > } > // if this item was marked strict, > // plus it cannot be resolve, and no value exist= , > bail out > throw new Exception\MissingPropertyException( > sprintf( > 'Missing %s for parameter ' . $name . ' > for ' . $class . '::' . $method, > (($value[0] =3D=3D=3D null) ? 'value' : > 'instance/object') > ), > $e->getCode(), > $e > ); > } else { > //finally ( be aware to do at the end of flow) > array_pop($this->currentDependencies); > if (isset($alias)) { > array_pop($this->currentAliasDependenencies)= ; > } > return false; > } > } catch (ServiceManagerException $e) { > // > Zend\ServiceManager\Exception\ServiceNotCreatedException > if ($methodRequirementType & self::RESOLVE_STRICT) { > //finally ( be aware to do at the end of flow) > array_pop($this->currentDependencies); > if (isset($alias)) { > array_pop($this->currentAliasDependenencies)= ; > } > // if this item was marked strict, > // plus it cannot be resolve, and no value exist= , > bail out > throw new Exception\MissingPropertyException( > sprintf( > 'Missing %s for parameter ' . $name . ' > for ' . $class . '::' . $method, > (($value[0] =3D=3D=3D null) ? 'value' : > 'instance/object') > ), > $e->getCode(), > $e > ); > } else { > //finally ( be aware to do at the end of flow) > array_pop($this->currentDependencies); > if (isset($alias)) { > array_pop($this->currentAliasDependenencies); > } > return false; > } > } > > To save everyone some time, the two code blocks for the two exception > types are completely identical. I'm not sure why it wasn't > function()alized, but regardless, it's another case where the new propose= d > feature would make perfect sense. > > I think saying that identical handling for multiple exception types is ba= d > or is grounds for an urgent code review is fairly disconnected from reali= ty > in many if not most cases. > > Zeev > > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php Patrick --001a114cbc648f1fee0530f1f11a--