I come from high-level languages with exceptions (C#, in particular), and I usually only catch exceptions that my logic knows how to handle — otherwise, they propagate and program crashes, but at least it provides a clear information about what happened, and where. I want to implement similar behavior in C. I want, in the small body of my main
, to get an error code from the functions that I call, to identify what happened, and to print error message to stderr
and quit with the same error code as I got.
First question — do people do that in C, and is it a good objective, in terms of architecture, at all?
Seconds question is about error return codes and errno. What should I use in my code by default? It seems that these two conventions are used interchangeably in C standard library, and it's pretty confusing.
Third question is about error codes themselves. In C# and Java, there are standard exception types that cover almost everything you would typically throw, apart from specific comments. Should I use errno error code table for this purpose, or should I create a separate error code table for my application to include all stuff that it specific to it? Or may be every function should maintain it's own specific table of error codes?
And finally, additional information about errors: I found that in C#, at least, it's very useful to be able to throw an exception of predefined type with a specific comment that provides additional information. Is something like this used in C error management? How should I implement it?
Why do library functions use return codes and errno at the same time instead of sticking to one or the other? What's the benefit in using both?
It seems, you can make use of errno
variable and perror()
functions. They help to provide the reason for failure for most of the time.
Most of the library functions, to indicate the failure, will usually return a prefixed negative value (mostly -1
) and set the errno
variable to a particular value, depending on the type of the error.
is it normally used for this purpose?
Yes, that is the purpose for having errno
and perror()
, IMHO.
Now, in case of user-defined functions, you can create own error codes for the errors returned by the library functions (with some time of mapping) and you can return your own value from your function.
EDIT:
What's the benefit in using both?
To differentiate the reason for failure, in case of a failure event, in an easy way.
Think it like this, instead of having a fixed reurn value for error and setting errno, if a function returns different values for each type of error, then how many if-else
or switch
statements you'll need for checking the success of the called function, each time it's called? It's huge.
Rather, use a fixed return value to indicate error and if error, then check the errno for the reason behind the error.
[Hope I'm clear. English is not my native language, sorry]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With