The only situation where an error handling code is not needed is redundant. Avoiding redundant error handling code is exactly the same as avoiding redundant code altogether. The only question is what might be the area for error handling code. Typically, there is so much common behavior in the greatest possible area.
For example, malloc failing is usually fatal, so instead of checking every return value, you can wrap the function ...
void* fmalloc(size_t n) { void* const m = malloc(n); if (!m) on_fatal("out of memory"); return m; }
If the behavior can only be limited by the calling function, you can use goto ...
int on_file(fd, rm, wm) { if (read(fd, rm, 8) < 0) goto err; if (write(fd, wm, 8) < 0) goto err; return 0; err: on_error("on_file error"); return -1; }
For things that can be parameterized, parameterize them.
An example that I am using is used here. In general, shortening error handling code is no different from simply grouping common behavior.
Jason source share