Hacker News new | ask | show | jobs
by marrs 3539 days ago
I wrote a simple set of c/++ macros to encapsulate this functionality, though it still relies on the dev to unwind in the correct order

#define HANDLE_ERROR(label) bool label = false; #define ENSURE(label, pred) if (!(pred)) { label = true; goto label; } #define RESOLVE(label) label: if (label)

HANDLE_ERROR statements go at the beginning of the block, RESOLVEs go at the end, in the reverse order to the ENSUREs, which go wherever they're required.

It's been working well for me so far.

1 comments

Let me try that again with formatting this time:

    #define HANDLE_ERROR(label) bool label = false;
    #define ENSURE(label, pred) if (!(pred)) { label = true; goto label; }
    #define RESOLVE(label) label: if (label)