Home

Search IconIcon to open search

Error handling in C

Important things - errno and perror. errno is an int in errno.h, and it’s set to various error codes if some function exits with an error. It’s zero otherwise. Check this out:

1
2
3
4
5
6
errno = 0
int wat = my_library_call()
if (errno != 0) {
	perror("my_library_call");
	exit(EXIT_FAULIRE);
}

You can find examples in the man pages of strtol, pipe, etc. Keep in mind that libraries don’t reset errno on success.

Using error codes is… Not that convenient. In Go it’s cooler!

See also Error handling.