r/C_Programming • u/heavymetalmixer • Jun 11 '26
Question C ways to manage errors?
I'm still learning C (I come from C++) and I'm not familiar with how to manage errors in C, besides asserts. What are the different ways to do this in C? How do they work?What are their pros and cons?
35
Upvotes
15
u/This_Growth2898 Jun 11 '26 edited Jun 11 '26
Two most common patterns of error handling in C are return error values and static error value. In both cases, "error" is usually an integer value (with the table describing it, maybe a function that returns a string description etc.). Return values look like this:
Of course, if you need something to be returned from the function, you need to pass it a pointer argument to that value.
Static values look like this:
where get_error_value returns the value of some thread local static variable, or even simply
Like, the standard library uses the
errnostatic variable.5
u/pjl1967 Jun 11 '26
assertis not for errors; they're for violating program invariants that in theory should "never" happen. Errors and assertions are two very different things.5
u/n4saw Jun 11 '26 edited Jun 11 '26
The classical approach is to return a non-zero value on error. That value can also then communicate what caused the error. If you’re designing a library, it is not uncommon to se some `strerror` analogue which takes your code and converts it to a human readable string. The advantage of this approach is that it is simple, idiomatic and relatively ergonomic.
```
int lib_foo(void)
{
int err;
err = lib_bar();
if (err)
return err;
if (some_condition)
return LIB_ERROR_XYZ;
// Could have LIB_OK defined to 0 also
return 0;
}
int main(void)
{
int err;
err = lib_foo();
if (err) {
fprintf(stderr, ”Errror: %s\n”, lib_strerror(err));
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
```
Some people like to add a utility macro:
#define RET_ON_ERR(err, expr) \
if ((err = (expr))) return err
int lib_foo(void)
{
int err;
RET_ON_ERR(err, lib_bar());
return 0;
}
This is not uncommon, but some people dislike that it hides the control flow.
A different approach is to only return whether an error occurred at all, but to also call a callback function with further information. This gives the ability to write a detailed error message, without deciding what the user does with that message. You could for example also pass a severity enum to the callback, which gives the user the ability to for example crash on certain errors in certain situations.
You can also take some `struct lib_error *` as an argument (or perhaps just `int *err`), and use it to report detailed error information, while only returning whether an error occurred at all. This has the advantage of keeping the API consistent for functions that return pointers and those who don’t.
You can take it as far as you like and record the entire call graph manually using various macros, if you’d like.
Edit: not sure why formatting is broken. I’ve tried both with indentation and backticks 🤷🏼♂️
4
u/Limp-Confidence5612 Jun 11 '26
Try to treat errors as normal states of your program and don't deal with them as special states.
3
u/zellforte Jun 11 '26
If you can, try to not have errors at all. Might sound crazy - lots of things can fail after all - but try and shift your thinking to what operations that you can do on your values and see if you can augment your data to make conceptual nil/empty/nop values that when passed to your functions will just do nothing.
For example: The classic case of opening and reading data from a file, the first question to ask is how much detail do you need to return to the user if that fails? Does the user care if the file does not exist, if it's not readable, if it's corrupted, etc or does the user just cares if they get some data vs no data? If it's the latter, just return an "empty blob of data" that the rest of the program can just "process" as do nothing ops - if you care about distinguishing "empty file" vs "file not found" you can add a tag bit to the blob, but again, only if that actually matters for the user interaction.
2
u/sthlmtrdr Jun 11 '26
I belive the standard way is to use errno, an 8 bit return code, uint8_t (unsigned char) where 0 means OK/Success and 1 and up to 255 is a specific error. Addition to that declare descriptive strings for each error code so one may use the standard error printing functions like perror(), etc then logging or writing to the console.
This is a good resource for beginners:
https://pubs.opengroup.org/onlinepubs/9699919799/
Select volume "System Interfaces" and "3. System Interfaces" to open the viewer.
2
u/JustBoredYo Jun 11 '26
I like to use a return oriented approach where every function returns an unsigned int that represents the current error state. It's similar to errno, but you can configure the values to your liking and use it in combination with errno.
Say you want to open a file in a function but the fopen() call fails. You can then return an integer value representing file IO failure and can use errno to check if the file wasn't found, if it is a directory, etc.
It's not entirely secure but then again, what is in C?
1
u/BubbleProphylaxis Jun 11 '26 edited Jun 11 '26
hmm... assert will just halt your program. not a great way to handle errors gracefully, but good for debugging. dont leave assert calls in production code.
Also, not sure what you mean -when you say you come from c++... as in you were using exception handling (throw/catch)? in C, error conditions need to be manually handled up the calling chain.
In any case, in C, function return values are often used to indicate error; eg, returning NULL, returning -1, etc. but that can sometimes get in the way of nice flowing code or semantics. For example, if you have a function that calculates a number, returning 0 or -1 might conflict with the result of the actual calculation. So in that case you might want to standardize your code using one of the two approaches below.
int sumPositives(int a, int b, ErrCode* err) {
*err = ERR_OK;
if (a < 0 || b < 0) { *err = ERR_FAIL; return 0; }
return a+b;
}
or
ErrCode sumPositives(int a, int b, int* val) {
*val = 0;
if (a < 0 || b < 0) { return ERR_FAIL; }
*val = a+b;
return ERR_OK;
}
Another interesting way to handle errors is to sort-of simulate exception handling, and that leverages the 2nd example above, always returning an error code, and of all things, using goto
(I know I'll get shot for this but this is an example -- this doesn't compile I just typed it from long-ago memory)
For example:
===========
// declare an error control variable
#define BEGIN() ErrorCode __err = ERR_OK;
#define RETURN() return __err;
// declare a macro that calls what's inside the (). assuming it returns an ErrCode.
// if the errorCode isn't OK, then it jumps to the catch block.
#define TRYCALL(f) __err = f; if (__err != ERR_OK) goto __catch_label;
// declare the goto __catch_label. Check the last __err value set. if all ok, jump the the finally block. If an error is set, enter the error handllng block.
#define CATCH __catch_err: if (__err == ERR_OK) goto __finally_label; else
// decare the end of the function,
#define FINALLY __finally_label:
Now you can do things like this:
ErrorCode SomeFunctionThatFails(int a, int b) { return ERR_FAIL; }
ErrorCode SomeFunctionThatWorks() { return ERR_OK; }
ErrorCode MyFunctionThatChecks() {
BEGIN;
TRYCALL( SomeFunctionThatWorks() ); // won't jump
TRYCALL( SomeFunctionThatFails(3,5) ); // will jump
TRYCALL( SomeFunctionThatWorks() ); // will never be called
CATCH {
// do your error handling here
}
FINALLY {
// do whatever cleanup here
}
RETURN();
}
....
In any case, there's a number of artciles online that discusses creating a system for error handling in C using gotos. it's ugly, people have said theres never any good use for it, but this is the one instance where I've found it actually works and works well and simplifies quite a few things, instead of adding if statements everywhere to check for a fault at every sub-function call.
This guy has a nice toolkit to do exceptions in C.
https://github.com/5cover/C-exCeptions
1
u/rb-j Jun 11 '26
I'm really old school. I used
printf()and would examine intermediate variables that weren't taking on the values I expected of them.1
u/Classic-Rate-5104 Jun 11 '26
It's all about the return value of a function. Some use True(1)/False(0), while others use OK(0)/nonzero where the nonzero indicates the type of error. Sometimes you see also >= 0 is normal behavior while < 0 is an error
2
u/genafcvpxyr31 Jun 12 '26
I'm learning C myself, what I've noticed is if the function returns a pointer, NULL is usually returned when an error occurs. If the function returns an integer, indicating an error is often done using a negative return value. They always involve setting errno to zero before the function call. All errno values are positive, so if you want to make custom error values it's best to create negative ones.
Some examples from linux:
int ferror(FILE*file) is an example of a function in the standard library to catch errors solely used for file streams, but you still need to set errno to get the exact error.
I often use char *strerror(int error) to geta string of the error description, but for learning about C if you're using linux I've found another function called strerrorname_np(int error). This function returns the error identifier as a string constant, for example if you tried to call a directory and it doesn't exist, strerrorname_np will return "ENOENT" or "ENOTDIR". If you wish to use this function you need to #define _GNU_SOURCE before including string.h, otherwise the compiler complains about implicit declaration.
If you don't already know, I found escape sequences for colour output on a terminal, I like to use red text for errors - "\e[38;2;255;0;0m" is used as a string or macro.
https://en.wikipedia.org/wiki/ANSI_escape_code#Select_Graphic_Rendition_parameters
There's ways to get full 24-bit colour control on a terminal, I think it's fairly portable. The general form for text colour is \e[38;2;(r);(g);(b)m and the form for background colour is \e[48;2;(r);(g);(b)m, with r, g and b taking values in decimal from 0-255. \e[0m resets the colour to default.
2
u/LavenderDay3544 Jun 13 '26
ERRNO
5
u/reines_sein Jun 11 '26
I think you basically account for the return value of functions in general, a certain return value, generally 1, means that the function failed at its task.
opencloseetc.0
u/my_password_is______ Jun 11 '26
become a better programmer,
don't make any errors
2
u/[deleted] Jun 11 '26
[removed] — view removed comment