1

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

2
  • 1
    do these error codes need to be "standard" errno error codes? If not, then why not just define and return your own error codes? Commented May 29, 2012 at 13:32
  • 1
    The no dynamic memory restriction does not exclude exceptions. In this day and age, in my opinion, it doesn't make sense to not use exceptions. Commented May 29, 2012 at 13:34

2 Answers 2

2

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) {
...
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

That's interesting, I tried to find some ressource for this but couldn't find any information besides the link you already wrote.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.