-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|677|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
SoCoder -> Snippet Home -> Misc


 
HoboBen
Created : 01 December 2012
 
System : Cross Platform
Language : Monkey

Better C error handling with goto and macros



I use gotos for error handling in C. (yes this is a valid use for gotos). It allows me to jump to any point in cleanup section.

For a good example, see StackOverflow: stackoverflow.com/questions/788903/valid-use-of-goto-for-error-management-in-c

It just occurred to me that I could throw in a macro and automatically get pretty error messages for every error handled, no extra work.

It gives me nice output like:

ced: ../src/canvas.c:183 OpenCanvas: Exception 'err_already_open' at line 183


Here's an example that checks for valid arguments, allocates some memory, and at any point can unwind from an error.



Just include this somewhere:


 

Comments