Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:92414 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 32416 invoked from network); 18 Apr 2016 15:17:45 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Apr 2016 15:17:45 -0000 Authentication-Results: pb1.pair.com header.from=derick@php.net; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=derick@php.net; spf=unknown; sender-id=unknown Received-SPF: unknown (pb1.pair.com: domain php.net does not designate 82.113.146.227 as permitted sender) X-PHP-List-Original-Sender: derick@php.net X-Host-Fingerprint: 82.113.146.227 xdebug.org Received: from [82.113.146.227] ([82.113.146.227:60564] helo=xdebug.org) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 77/88-11975-69AF4175 for ; Mon, 18 Apr 2016 11:17:43 -0400 Received: from localhost (localhost [IPv6:::1]) by xdebug.org (Postfix) with ESMTPS id 15B8EDF5F3; Mon, 18 Apr 2016 16:17:40 +0100 (BST) Date: Mon, 18 Apr 2016 16:17:39 +0100 (BST) X-X-Sender: derick@whisky.home.derickrethans.nl To: Marco Pivetta cc: Dan Ackroyd , =?UTF-8?Q?Bronis=C5=82aw_Bia=C5=82ek?= , PHP internals In-Reply-To: Message-ID: References: User-Agent: Alpine 2.20 (DEB 67 2015-01-07) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Subject: Re: [PHP-DEV] [VOTE] Catching Multiple Exception Types From: derick@php.net (Derick Rethans) On Mon, 18 Apr 2016, Marco Pivetta wrote: > Heya, > > I voted "NO" due to previous discussion. TL;DR: this is FAR off the 80/20 > use-case for a language syntax change. I'm with you on this one. > C&P from my answer elsewhere: > > The following typical example is something REALLY rare, and requiring a > parser change for it seems excessive: > > try { > // ... > } catch (InvalidArgumentException $e) { > // same handling > } catch (PDOException $e) { > // same handling > } catch (BadMethodCallException $e) { > // same handling > } > > These 3 exceptions usually result in separate handling anyway. If same > handling is needed, you can as usual extract a private method (if you are > in a class) and deal with it there: > > try { > // ... > } catch (InvalidArgumentException $e) { > $this->sameHandling($e); > } catch (PDOException $e) { > $this->sameHandling($e); > } catch (BadMethodCallException $e) { > $this->sameHandling($e); > } > > private function sameHandling(Throwable $caught) > { > // same handling > } Even this is a scary way to do it. As you don't know which exception class you have, how would you know how to handle each separately. You'd have to if ($caught instanceof InvalidArgumentException) etc to be sure that what you're doing with the object is allowed. If they are user defined exceptions, you should make them implement an interface, and do a "catch (IMyException $e)" to be sure you can handle the different exceptions in a consistent manor. And that already works. If they are exception classes defined by other libraries, then, as you say, it's very likely they should be handled differenty anyway. Or rather, very generically, which you can do with "catch (Exception $e)". cheers, Derick -- http://derickrethans.nl | http://xdebug.org Like Xdebug? Consider a donation: http://xdebug.org/donate.php twitter: @derickr and @xdebug Posted with an email client that doesn't mangle email: alpine