Skip to content

Instantly share code, notes, and snippets.

@ericsonj
Last active May 7, 2021 19:42
Show Gist options
  • Save ericsonj/2b799a0abe922a9d617db926deff68c8 to your computer and use it in GitHub Desktop.
Save ericsonj/2b799a0abe922a9d617db926deff68c8 to your computer and use it in GitHub Desktop.
c_exceptions_handler
#include "Exceptions.h"
FILE* fd = NULL;
TRY {
// try open file and do something
fd = fopen("file.txt", "r");
THROW_IF_FAIL(fd != NULL, Exception);
// more lines
} CATCH(ex, Exception) {
// Jump to here from any invalid THROW_IF_FAIL
printf("error");
} FINALLY {
// If all is OK or NOT, run finally
if (fd != NULL) {
close(fd); // Close file
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment