Re: In a destructor, how to detect if an exception is currently in flight?
| From: | Johannes Schlüter | Date: | Wed, 28 Oct 2015 11:13:34 +0000 |
| Subject: | Re: In a destructor, how to detect if an exception is currently in flight? | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-88963@lists.php.net to get a copy of this message | ||
On Wed, 2015-10-28 at 11:00 +0100, Erik van Velzen wrote:
> Subject says it all.
>
> I want to make a small "ScopeGuard" library for exception-safe
> transactions, like the following:
>
> $scope = new ScopeGuard;
>
> $scope->onSucces(function() { logTransactionOne(); });
> $scope->onFailure(function() { rollbackTransationOne(); });
> doTransactionOne();
>
> $scope->onSuccess(function() { logTransactionTwo(); });
> $scope->onFailure(function() { rollbackTransactionTwo(); });
> $scope->onFailure(function() { logFailureOfTransactionTwo(); });
> doTransactionTwo();
>
> // ... Three Four etc...
>
> So if an exception is thrown the attached onFailure callbacks will be
> executed, if the scope is exited through a return however the success
> callbacks will be executed.
The way to do this is by marking the success in an explicit way. See
http://slides.com/johannes4321/froscon15/#/4
and
http://slides.com/johannes4321/froscon15/#/6
With your attempt you can only handle abortion via exception. With a
pattern like shown in my slides you can also handle cases where you
"return" early nicely.
johannes
Attachment: [application/pgp-signature] This is a digitally signed message part signature.asc
Attachment: [application/pgp-signature] This is a digitally signed message part signature.asc