Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:88965 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 83598 invoked from network); 28 Oct 2015 14:34:34 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 28 Oct 2015 14:34:34 -0000 Authentication-Results: pb1.pair.com smtp.mail=inefedor@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=inefedor@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.217.169 as permitted sender) X-PHP-List-Original-Sender: inefedor@gmail.com X-Host-Fingerprint: 209.85.217.169 mail-lb0-f169.google.com Received: from [209.85.217.169] ([209.85.217.169:36021] helo=mail-lb0-f169.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 3E/32-63642-9FCD0365 for ; Wed, 28 Oct 2015 09:34:33 -0500 Received: by lbjm5 with SMTP id m5so7101438lbj.3 for ; Wed, 28 Oct 2015 07:34:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=content-type:to:subject:references:date:mime-version :content-transfer-encoding:from:message-id:in-reply-to:user-agent; bh=phnk90zcOZiTnwfq98XEjWw9BUMRh8qTeCc/8xKJ/Yg=; b=kjwIlBADPPXwfF+BAq1tK/hjrhv2jKanst7U3XnpFymadW4geRQaNVnmZnBjnw2CAu O9e0KAbwndI3bjcfGOxXj0KPqJvcniGs+FPwNAzx7aBaRBclSrsvale36E7iiv36zZu5 uEELKavNUT3CzQgIaAw88mehezXateWLPm2B5noB2IvoBu6oCIPg3cqeJ3If3Ev7EtEW oiJAZtkiSnjghuVqJ21lcfnkvXJX+NnvRCla1xosEDrdXcYZQcV/SpLl5p7Q7OE2CrTu RtFBEHDE/3Y2IrYwOeTfu+e3fRPNGdIo0ZIXnsNlREhfw6jYSl+MKfTd25PQqqUXtKb1 zk/w== X-Received: by 10.112.137.169 with SMTP id qj9mr23593724lbb.34.1446042870153; Wed, 28 Oct 2015 07:34:30 -0700 (PDT) Received: from nikita-300e4c-300e5c-300e7c ([185.62.192.230]) by smtp.gmail.com with ESMTPSA id um10sm7863951lbc.15.2015.10.28.07.34.28 (version=TLSv1 cipher=RC4-SHA bits=128/128); Wed, 28 Oct 2015 07:34:29 -0700 (PDT) Content-Type: text/plain; charset=utf-8; format=flowed; delsp=yes To: internals@lists.php.net, "Erik van Velzen" References: Date: Wed, 28 Oct 2015 17:34:28 +0300 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Message-ID: In-Reply-To: User-Agent: Opera Mail/12.16 (Linux) Subject: Re: [PHP-DEV] In a destructor, how to detect if an exception is currently in flight? From: inefedor@gmail.com ("Nikita Nefedov") Hi Erik, On Wed, 28 Oct 2015 16:28:59 +0300, Erik van Velzen wrote: > Thanks for your input. > > I did not try-catch because it doesn't really work, you either > duplicate rollback code or get excessive nesting. Real code will > obviously be more complicated than the examples. > Also in both cases the handlers are many lines apart from the actual > transaction call. > > Duplicate rollback code: > > try { > transactionOne(); > } > catch (\Throwable $e) { > rollbackTransactionOne(); > throw $e; > } > > try { > transactionTwo(); > } > catch (\Throwable $e) { > rollbackTransactionOne(); > rollbackTransactionTwo(); > throw $e; > } > finally { > cleanupTransactionTwo(); > } > > try { > transactionThree(); > } > catch (\Throwable $e) { > rollbackTransactionOne(); > rollbackTransactionTwo(); > rollbackTransactionThree(); > throw $e; > } > > logTransactionOne(); > logTransactionTwo(); > logTransactionThree(); In C goto is usually used for this kind of problem, this is how it's usually done: https://gist.github.com/nikita2206/dc48c9f14db61420751e here's a first article I could find about it http://eli.thegreenplace.net/2009/04/27/using-goto-for-error-handling-in-c What you could also use though is a generator function: https://gist.github.com/nikita2206/64959e63ebb49d6ce0c8