I have a few functions that return a 1 if an error is encountered. Each function calls on a lower-level function, such that if the lower-level function returns a 1, the original function returns a 1 as well. Thus errors get passed up the chain in this way.
Here’s an highly abridged version of one of these functions:
if (low_level_function()) {
[do stuff]
return 1;
}
[do other stuff]
return 0;
Should I instead declare an error variable, assign the result of low_level_function() to it, and then use the error variable in the if() statement? In other words:
int error = low_level_function();
if (error) {
[do stuff]
return 1;
}
[do other stuff]
return 0;
Or is there yet another, better way of doing this? I’ve never coded to account for errors before, so my experience here is rather limited.
Edit: I’ve reformatted the functions to better convey the nature of my code.
I dont see the difference between the two approaches above.
I would recomment using exception, much more cleaner approach. why the reinvent the wheel? You can either use standard exception or implement custome exception like