Home > database >  Error handling with "err" in C: What codes should I use?
Error handling with "err" in C: What codes should I use?

Time:05-16

The man page for err and errx states the following syntax:

  • void err(int eval, const char *fmt, ...);
  • void errx(int eval, const char *fmt, ...);

Obviously const char *fmt is the format string and ... contains the parameteres for it (just like printf). I know that eval is a code you assign to the error. Since the man page doesn't say anything more specific about the eval parameter, I was wondering:
Is there a rule/ convention, which specifies what error codes should be assigned for specific errors, or it is a choice of the programmer, who is writing the code, to write whatever eval value they deem necessary?

CodePudding user response:

From the man page on my Ubuntu (GNU/Linux) system:

The err(), verr(), errx(), and verrx() functions do not return, but exit with the value of the argument eval.

So it's a value that you would pass to exit.

If you want to be portable, C defines EXIT_SUCCESS and EXIT_FAILURE.

That said, you should have no problems returning a number from 0 to 127, with 0 meaning success. The meaning of those numbers is up to you, but usually go up in severity. For example, grep returns 0 if a match is found, 1 if a match isn't found, and 2 if an error occurred.

  • Related