Re: In a destructor, how to detect if an exception is currently in flight?
| From: | Erik van Velzen | Date: | Wed, 28 Oct 2015 15:02:11 +0000 |
| Subject: | Re: In a destructor, how to detect if an exception is currently in flight? | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-88967@lists.php.net to get a copy of this message | ||
I didn't see how it could be generalized but your example clears that up.
- Erik
2015-10-28 15:40 GMT+01:00 Rowan Collins <rowan.collins@gmail.com>:
>
> Did you see Johannes' suggestion re explicit success vs implicit failure?
>
> $scope = new ScopeGuard;
>
> $scope->onSucces(function() { logTransactionOne(); });
> $scope->onFailure(function() { rollbackTransationOne(); });
> doTransactionOne();
>
> $scope->onSuccess(function() { logTransactionTwo(); });
> $scope->onFailure(function() { rollbackTransactionTwo(); });
> $scope->onExit(function() { cleanupTransactionTwo(); });
> doTransactionTwo();
>
> $scope->registerSuccess();
>
>
> The ScopeGuard's destructor can now detect the condition:
>
> if ( $this->success_registered ) {
> // Function reached checkpoint for this scope
> $this->callSuccessHandlers();
> } else {
> // Function aborted early, due to a throw or early return
> $this->callExitHandlers();
> }
> $this->callExitHandlers();
>
>
> No boilerplate throw-catch needed, works even if there is already a catch or
> finally within the function's definition.
>
> In fact, I kind of want to use this somewhere now... ;)
>
> Regards,
> --
> Rowan Collins
> [IMSoP]
>