Re: In a destructor, how to detect if an exception is currently in flight?

From: Date: Wed, 28 Oct 2015 14:34:28 +0000
Subject: Re: In a destructor, how to detect if an exception is currently in flight?
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-88965@lists.php.net to get a copy of this message
Hi Erik, On Wed, 28 Oct 2015 16:28:59 +0300, Erik van Velzen <erik@evanv.nl> 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

« previous php.internals (#88965) next »