M
mast2as
Hi everyone... I have a TExceptionHandler class that is uses in the
code to thow exceptions. Whenever an exception is thrown the
TExceptionHander constructor takes an error code (int) as an argument.
I was hoping to create a map<int, const char*> that would be used in
the showError member function of the TExceptionHandler class where the
key (int) would be the error code and const char* the message printed
out to the console.
My question is the following. What would be the best way of creating
that map ? Here is why I am asking. I believe when you throw an
exception you actually create a copy of the object TExceptionHandler.
So if the map is a member variable of the class and that each entry in
the map is intialised with a string (as an error message) it would be a
waste of CPU time & memory (just stupide to create that list eveytime a
TExceptionHandler object is created).
Is the best way, to make the TExceptionHandler class a singleton ? The
map could a static member variable and would be intialised only once.
Or should I make the map a global variable (like an array of const
char* or string) and just use that global variable in the showError()
function ?
I am sure there's a good way of doing that ;-)
I looked on the net and the groups' archives but couldn't find anything
related on that precise aspect of exception handling.
thank you, mark
(ps I know i ask a lot to the group at the moment, but i also learn a
lot from you guys ! I hope i'll be able to help other people one
day...)
class TExceptionHandler
{
public:
TExceptionHandler( int error ) :
m_code( error ),
m_severity( GIE_ERRORPRINT ),
m_message( "" )
{}
TExceptionHandler( int error, int severity ) :
m_code( error ),
m_severity( severity ),
m_message( "" )
{}
~TExceptionHandler() {}
void showError()
{
// print out the error message base of the error code
cout << globalErrorMessageList[ m_error ] << endl; << is that the
best option ?
}
private:
int m_code;
int m_severity;
const char* m_message;
};
// The class used somewhere in the code
try {
popModel( TAppliState::kModelA );
} catch ( TExceptionHandler &e ) {
e.showError();
}
code to thow exceptions. Whenever an exception is thrown the
TExceptionHander constructor takes an error code (int) as an argument.
I was hoping to create a map<int, const char*> that would be used in
the showError member function of the TExceptionHandler class where the
key (int) would be the error code and const char* the message printed
out to the console.
My question is the following. What would be the best way of creating
that map ? Here is why I am asking. I believe when you throw an
exception you actually create a copy of the object TExceptionHandler.
So if the map is a member variable of the class and that each entry in
the map is intialised with a string (as an error message) it would be a
waste of CPU time & memory (just stupide to create that list eveytime a
TExceptionHandler object is created).
Is the best way, to make the TExceptionHandler class a singleton ? The
map could a static member variable and would be intialised only once.
Or should I make the map a global variable (like an array of const
char* or string) and just use that global variable in the showError()
function ?
I am sure there's a good way of doing that ;-)
I looked on the net and the groups' archives but couldn't find anything
related on that precise aspect of exception handling.
thank you, mark
(ps I know i ask a lot to the group at the moment, but i also learn a
lot from you guys ! I hope i'll be able to help other people one
day...)
class TExceptionHandler
{
public:
TExceptionHandler( int error ) :
m_code( error ),
m_severity( GIE_ERRORPRINT ),
m_message( "" )
{}
TExceptionHandler( int error, int severity ) :
m_code( error ),
m_severity( severity ),
m_message( "" )
{}
~TExceptionHandler() {}
void showError()
{
// print out the error message base of the error code
cout << globalErrorMessageList[ m_error ] << endl; << is that the
best option ?
}
private:
int m_code;
int m_severity;
const char* m_message;
};
// The class used somewhere in the code
try {
popModel( TAppliState::kModelA );
} catch ( TExceptionHandler &e ) {
e.showError();
}