Skip to content

Instantly share code, notes, and snippets.

@ekiru
Created April 4, 2011 22:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ekiru/902605 to your computer and use it in GitHub Desktop.
Save ekiru/902605 to your computer and use it in GitHub Desktop.
An example of C error handling with goto.
int main(int argc, char **argv) {
foo_error error;
foo_bar bar;
error_init(&error);
if (!foo_init(&error))
goto foo_init_error;
if (!foo_bar_init(&bar, "baz", &error))
goto bar_init_error;
if (!foo_bar_do_it(&bar, &error))
goto bar_do_it_error;
bar_do_it_error:
foo_bar_cleanup(&bar);
foo_init_error:
foo_cleanup();
if (error->occured) {
error_display(&error);
}
error_cleanup(&error);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment