C errors/exceptions

From wikinotes
Revision as of 01:48, 16 June 2016 by Will (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


system errors (errno/perror)

When syscalls produce errors, they return -1, and set the global constant errno. <errno.h>'s perror is a function that when provided with an errno, returns the error that was produced.


See man errno for a list of exit() codes and their meanings.


#include <errno.h>

void error( const char *message ){
	if (errno) {
		perror( message );
	} else {
		printf("ERROR: %s\n", message);
		exit(1);
	}
}


exceptions

C does not have any exceptions.