views:

52

answers:

2

I have an application with some bunch of code like this:

errCode = callMainSystem();
switch (errCode){
    case FailErr: error("Err corresponding to val1\n");
    case IgnoreErr: error("Err corresponding to val2\n");
    ...
    ...
    default: error("Unknown error\n");
}

The values are enum constants. Will it make some sense to have something like:

// Error* callMainSystem() 
... Some code
return FaileErr(); // or some other error

// handling code
Error* err = callMainSystem();
err->toString();   

The Error class may be made singleton as it only has to print error messages.

What are the pros and cons of above methods,size is an important criteria as the application needs to be supported on embedded devices as well.

P.S: I don't want to use exception handling because of portability issues and associated overheads.

+2  A: 

IMHO polymorphism would be overkill here, as you don't actually need different behaviour, only use different data per error code. I would use a simple map<ErrorCode, string> instead to store the mappings from error codes to messages.

The polymorphic solution would require the extra overhead of creation and destruction of Error instances. I can't see how to make Error a singleton; however, you could implement it as a sort of typesafe enum like in Java (i.e. a fixed number of constant instances). However, hardwiring their behaviour (e.g. printing error messages to stderr) can make it awkward to unit test your code.

Péter Török
yeah.. mapping is a good solution. :)
Neeraj
@Neeraj Except that std::map operations may throw exceptions.
anon
yeah.. I knew that would come :P.. thats why I have mentioned mapping (not maps exactly).By mapping i meant a name value pair for which luckily we do have a class :).
Neeraj
+4  A: 

When you say:

I don't want to use exception handling because of portability issues and associated overheads.

There really aren't any, assuming an even moderately up-to-date compiler. All you are doing is reproducing in your code what the C++ compiler will do for you anyway.

anon
so far,we don't have std library code because of size issues. The library exceptions are generic and generalizations IMO do add overheads.I respect your experience, feel free to correct me :).
Neeraj
@Neeraj So your environment is what the standard calls "free standing"? I don't think that even in that case it can avoid implementing exception handling. Perhaps if you say exactly what your C++ implementation is?
anon
It is basically C with OOP, with wrapper classes over various functionalities as required. Error handling is so far done as indicated in the first example. All i wanted to make sure is if it can be made more convenient and cleaner and not adding any overhead to existing system.
Neeraj
@Neeraj You misunderstand me - what compiler are you using?
anon
as the application needs to be portable,it should be compilable using various compilers g++, MSVC.
Neeraj
@Neeraj All of which use and depend on exceptions for handling error conditions. Why on earth do you think your code should not use the same language feature? I routinely port code from Windows to Linux that uses exception handling.
anon