views:

83

answers:

2

I am designing a software that has two logical layers, say A and B. Layer B has set of objects (classes). Layer A uses set of APIs exposed by layer B objects for information retrieval. I want to design a generic error interface across layer B objects that can be exposed to layer A. What would be the best way to write a generic error interface ?

Would adding enums for error interfacing suffice ?

Also,

For sample signature for Layer B APIs can be one of following:

error_interface Sample_API(args, void *return_value);

OR

(This is return value)void * sample_API(args, error_interface *err);

Which one of the following two signatures would be preferred ? & Why ?

Adding to this,

There will be few error codes those will be common for all layer B objects. Few error codes will be specific to some of B objects. How to create error hierarchy here ? I guess enums can not be used for creating error hierarchy ? Does creating error class hierarchy good idea ?

+2  A: 

Traditionally, error codes are often returned as return value, if any. Choose whatever you like best.

As an error interface, you could use exception type trees instead of an enumeration. For example, std::runtime_error > BaseExceptionOfB > SomeSpecificErrorOfB. Exceptions have speed disadvantages, but I often use them because they must be handled. Handling return values must always happen explicitly - i.e. if you, as the programmer, forget to check for an error, nobody will notice.

If you want to look at the C++ standard exceptions, this might be a good starting point, showing how to derive an exception type from std::runtime_error and catch exceptions of that type.

AndiDog
+1  A: 

If you are designing a new C++ API in 2010, you should of course use exceptions for notification of errors. There may be as well functions returning values etc, but the best way to report problems (and their deeper cause) as well as to handle them are exceptions.

Sadly stackoverflow does not allow "let me google that for you" anymore in answers.

A first search reveals i.e.:

http://www.cplusplus.com/doc/tutorial/exceptions/

http://www.flipcode.com/archives/C_Exception_Handling.shtml

RED SOFT ADAIR
can you point me to some open source cpp code that uses exception for error handling ?
cppdev
See references added to my post.
RED SOFT ADAIR