-
-
Save johanoloflindberg/fc23a99982bbbfb1e5e13e1cb961afde to your computer and use it in GitHub Desktop.
Try/finally cleaner than multiple gotos with label?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void myfunc() { | |
void *thing1 = NULL; | |
void *thing2 = NULL; | |
@try { | |
thing1 = allocThing1(); | |
if (!thing1) return; | |
// …use thing1 maybe | |
if (somethingWentWrongWithThing1) return; | |
// …use thing1 more maybe | |
thing2 = allocThing2(); | |
if (!thing2) return; | |
// …use thing2 and maybe thing1 | |
if (somethingWentWrongWithThing2) return; | |
// …use thing2 more and maybe thing1 more | |
} @finally { | |
if (thing1) deallocThing1(thing1); | |
if (thing2) deallocThing2(thing2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment