r/cprogramming • u/KweHuu • Jun 01 '26
How to handle errors correctly?
I'm currently making library with different data structures and I'm curious which way of error handling is considered better?
Also if you have any tips or guides how to make objectively good code/library I will be grateful.
Here I'm returning true or false to indicate whether the operation was successful.
bool stack_push(stack* stack, void* new_data){
if(stack == NULL || new_data == NULL) return false;
stack_node* new_node = malloc(sizeof(stack_node));
if(new_node == NULL) return false;
if(stack->byom){
new_node->data = new_data;
}else{
new_node->data = malloc(stack->data_size);
if(new_node->data == NULL){
free(new_node);
return false;
}
memcpy(new_node->data, new_data, stack->data_size);
}
new_node->next = stack->top;
stack->top = new_node;
stack->stack_size++;
return true;
}And here I'm returning custom enum which indicates what gone wrong.
stack_errno_e stack_push(stack* stack, void* new_data){
if(stack == NULL || new_data == NULL) return STACK_ERR_NOT_FOUND;
stack_node* new_node = malloc(sizeof(stack_node));
if(new_node == NULL) return STACK_ERR_ENTRY_ALLOC;
if(stack->byom){
new_node->data = new_data;
}else{
new_node->data = malloc(stack->data_size);
if(new_node->data == NULL){
free(new_node);
return STACK_ERR_DATA_ALLOC;
}
memcpy(new_node->data, new_data, stack->data_size);
}
new_node->next = stack->top;
stack->top = new_node;
stack->stack_size++;
return STACK_ERR_OK;
}
7
Upvotes
11
u/clickyclicky456 Jun 01 '26
It all depends what you want your callers to do. Will they need to react differently to different errors? If so then use the enum (and make sure STACK_ERR_OK is 0 so they can quickly check for OK with a boolean check), and write unit tests for each error. If not then just return a simple boolean and reduce your documentation and test overheads.
4
u/WittyStick Jun 02 '26 edited Jun 02 '26
A way that is unconventional in classic C, but works well in C23, is to use tagged union return types like are present in many other languages.
You could use an
Optiontype instead of a boolean result and error code, or boolean result and out parameter if we need to return anything. In the case ofstack_push, returning the error code is probably fine - in this case it is unnecessary because one of our error codes indicates success - but I'll use anOptionfor the return type to demonstrate, as it can be used in cases where we have a separate return value and a boolean condition to indicate success - and later we'll see how we can return an error code and a resulting value forstack_pop.One caveat of this is we can't use
Option(enum stack_errno_e)- we have to typedef first, but this really is a minor issue.To use this, we can have a couple of macros:
if_someandif_none. The preferred approach would be:Or alternatively:
get_somecan be considered a bit of a code smell because we might accidentally call it on anOptionthat doesn't have some - resulting in probably a zero value. Instead it might be better to use:Finally there's
maybewhich takes a default value if the result is none, otherwise returns the value provided by Some.Sometimes we only want to know if an error occurred and can ignore the actual result:
The macros for an Option type are very simple:
To extend this for non-boolean results where we might want an error code OR a resulting value, we would want a
Resulttype.I would suggest for this, if you use, to always set code
0to the no-error case so it can be handled uniformly. This avoids having to have a separate tag field to specify whether it's an error or a successful result.To use this is pretty much identical to the above approach for options, except we now also have a
get_error.We might want to avoid
get_okandget_errorand instead have anel_okandel_errorA slightly more ergonomic way would be to extract the value and switch on the error codes, with the default case being
OkThe macros for this
Resulttype are similar to the option and still trivial:You can extend these a bit with some more useful functionality. They're not perfect, but quite close to what you'd expect from an
OptionorResulttype in other languages. In fact, this may be slightly better than most languages implementations because it has zero-cost.We can also encapsulate these types with a GCC extension, which prevents them being used in any other way than the macros we've provided.
0
u/[deleted] Jun 01 '26
[deleted] — view deleted comment