 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Andy Guest
|
Posted: Wed Sep 28, 2005 5:42 pm Post subject: Mapping error codes to error messages |
|
|
I need to make an error handling framework in which application error
codes can be mapped to pre-defined error messages. The framework would
have C++ exception classes for error information propagation. I however
have no clue on how to map the error codes to the messages, and
efficiently access the messages through a simple mapping class or
function. I know this is not exactly standard C++ that we are
discussing, but is anybody aware of any implementation techniques.
Should it necessarily be a static array indexed by error codes or some
rehash of the error codes.
Cheers,
Andy
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dan McLeran Guest
|
Posted: Wed Sep 28, 2005 10:03 pm Post subject: Re: Mapping error codes to error messages |
|
|
I would use something like std::map<unsigned int, std::string>. The
unsigned int would be the error code and the std::string would be the
message for that error code.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Jeff Schwab Guest
|
Posted: Thu Sep 29, 2005 2:56 pm Post subject: Re: Mapping error codes to error messages |
|
|
Andy wrote:
| Quote: | I need to make an error handling framework in which application error
codes can be mapped to pre-defined error messages. The framework would
have C++ exception classes for error information propagation. I however
have no clue on how to map the error codes to the messages, and
efficiently access the messages through a simple mapping class or
function. I know this is not exactly standard C++ that we are
discussing, but is anybody aware of any implementation techniques.
Should it necessarily be a static array indexed by error codes or some
rehash of the error codes.
|
I generally store my error messages in an array, and use error codes as
indexes into the array. My error codes are usually enumerations; I
don't know whether enumerations as array indexes are quite kosher, but
they have worked for me.
If your error codes are distributed across a large space of integers,
you might use them as keys to a switch statement, with one case label
for each error code.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze Guest
|
Posted: Thu Sep 29, 2005 8:31 pm Post subject: Re: Mapping error codes to error messages |
|
|
Dan McLeran wrote:
| Quote: | I would use something like std::map<unsigned int,
std::string>. The unsigned int would be the error code and the
std::string would be the message for that error code.
|
Depending on how the error codes and messsages are being
maintained, char const*[] is likely the simplest solution.
(Note that if errors may occur during the construction of static
objects, a solution which uses static initialization is almost a
necessity. So no std::string, no std::vector and no std::map.)
Of course, if the error codes are pre-defined, and very sparce,
this may not work. If order of initialization is not a problem,
std::map is a very good solution. Otherwise, I've found that
something like:
static struct MessageMap
{
unsigned int code ;
char const* message ;
} const map[] =
{
{ Error1, "whatever went wrong" },
// ...
}
works well. Most of the time, a simple linear search
(std::find) is sufficient; if not, it's generally not too hard
to sort the table and use std::lower_bound on it.
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maciej Sobczak Guest
|
Posted: Thu Sep 29, 2005 8:31 pm Post subject: Re: Mapping error codes to error messages |
|
|
Andy wrote:
| Quote: | I know this is not exactly standard C++ that we are
discussing
|
It *is* a standard stuff, see std::messages class.
The fact is that I've never seen it used in practice - probably because
there are too many things that are left "implementation-defined".
| Quote: | is anybody aware of any implementation techniques.
|
Go for the std::map solution as already suggested, it is easy to
implement and quite extensible as well.
--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
adebaene@club-internet.fr Guest
|
Posted: Thu Sep 29, 2005 8:32 pm Post subject: Re: Mapping error codes to error messages |
|
|
Andy wrote:
| Quote: | I need to make an error handling framework in which application error
codes can be mapped to pre-defined error messages. The framework would
have C++ exception classes for error information propagation. I however
have no clue on how to map the error codes to the messages, and
efficiently access the messages through a simple mapping class or
function. I know this is not exactly standard C++ that we are
discussing, but is anybody aware of any implementation techniques.
Should it necessarily be a static array indexed by error codes or some
rehash of the error codes.
|
Do you have any internationalization concern (ie, do the error messages
need to be localized)?
If yes, the map <Error Code> <--> <Error Message> should be in some
kind of external resource (a file, a database, whatever is available on
your environment) and you should havesome kind of framework to retrieve
the correct translation, based on the current locale.
Arnaud
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|