#Error Handling

6 messages · Page 1 of 1 (latest)

worldly rivetBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

vestal lark
#

One way of dealing with it in C, if you really have no other choice, is to use errno. You can think of errno as a global variable that you can get by including errno.h.

You can then specify a value that should indicate a possible error, say 0.
What this ends you up with is that you can trust any value other than 0 to be definitely legit, no errors, but if you get 0 you just need to check if errno is set, and if yes, to what value, and depending on that value you can do error handling.

#

what if I check before I even call that pop function
That also works.

#

Another alternative would be to return a struct like so:

typedef struct uint16_optional u16_opt;
struct uint16_optional {
    uint16_t value;
    bool exists;
};

u16_opt example_stack_pop_uint16() {
    if (stack_under_flow) {
        return { .value=0, .exists=false };
    }

    return { .value=pop_value, .exists=true };
}
#

*Manually setting errno is usually not what you want to do, but I figured I'd drop it nonetheless as it's quiet commonly used by the standard libraries' functions

worldly rivetBOT
#

This question is being automatically marked as stale.
If your question has been answered, type !solved.
If your question is not answered feel free to bump the post or re-ask.
Take a look at !howto ask for tips on improving your question.