Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save johanoloflindberg/fc23a99982bbbfb1e5e13e1cb961afde to your computer and use it in GitHub Desktop.
Save johanoloflindberg/fc23a99982bbbfb1e5e13e1cb961afde to your computer and use it in GitHub Desktop.
Try/finally cleaner than multiple gotos with label?
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