Created
April 4, 2011 22:28
-
-
Save ekiru/902605 to your computer and use it in GitHub Desktop.
An example of C error handling with goto.
This file contains 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
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