R – Using callback functions for error handling in C


I have been thinking about the difficulty incurred with C error handling.. like who actually does

if(printf("hello world")==-1){exit(1);}

But you break common standards by not doing such verbose, and usually useless coding. Well what if you had a wrapper around the libc? like so you could do something like..

printf("hello world"); //this will automatically call my_errors on an error of printf
ignore=1; //this makes it so the function will return like normal and we can check error values ourself
if(fopen.... //we want to know if the file opened or not and handle it ourself.

int my_errors(){
      _exit(1); //exit if we aren't handling this error by flagging ignore
return 0;
//this is called when there is an error anywhere in the libc

I am considering making such a wrapper as I am synthesizing my own BSD licensed libc(so I already have to touch the untouchable..), but I would like to know what people think about it..
would this actually work in real life and be more useful than returning -1?

Best Solution

But how would you be able to catch the error when it was expected? For example I might expect a file open to fail and want to deal with it in code instead of the generic error catcher.

To do this you would need two versions of every function. One that trapped errors and one the returns errors.

I did something like this long ago without modifying the library. I just created wrapper functions for common calls that did error checking. So my errchk_malloc call checked the return and raised an error if the allocation failed. Then I just used this version everywhere in place of the built in malloc.