Ahnihmuhs
Ahnihmuhs

Reputation: 107

How to define custom error codes in C(++)

I'm desperately trying to define custom error codes in C(++).

As for the project I'm working on it's forbidden to use Exceptions and Signals and any other approach which could allocate dynamic memory.

I used the standard error codes for some methods which produce errors resembling those but for some methods the errors are to specific to be covered by the standard error codes the OS is providing.

I also searched for the error strings in my system but couldn't manage to find the error messages O_O Where are those defined anyway?

So I'm searching for any method which allows me to define a custom error code (e.g. 666) and the correspondig error message ("Satan declared an error!") which will be outputted using strerror. Is this possible or do I have to meddle with some system related files?

In best regards, ahnimuhs

Upvotes: 1

Views: 4712

Answers (2)

mouviciel
mouviciel

Reputation: 67889

Some implementations of strerror(3) allow for user defined error codes and labels.

You have to provide a _user_strerror() function and define error codes after __ELASTERROR.

Upvotes: 1

justin
justin

Reputation: 104708

If you declare an enum as a type, that can satisfy the domain where typesafety propagates.

Then you can offer a function to access the description as a char buffer (given a locale?, the current locale?).

class t_mon_io_error {
public:
enum t_type {
...
  SatanDeclaredAnError = 666
...
};

static const char* Description(const t_type& pError) {
  switch(pError) {
...
}

Upvotes: 2

Related Questions