M
Master of C++
Hi,
I am an absolute newbie to Exception Handling, and I am trying to
retrofit exception handling to a LOT of C++ code that I've written
earlier. I am just looking for a bare-bones, low-tech exception
handling mechanism which will allow me to pass character information
about an error and its location from lower-level classes.
Can you please critique the following exception handling mechanism in
terms of my requirements ?
I have defined an Exception base class as follows:
#define expMAX_MSG_LENGTH 512
class Exception
{
char Message[expMAX_MSG_LENGTH];
public:
Exception()
{
strcpy(Message, "Exception !");
}
Exception(const char *errMsg1, const char *errMsg2 = NULL)
{
// I am using assert()'s here because it is truly a programming
// error to allow the string lengths to exceed 512 bytes.
if (errMsg2 == NULL)
{
assert(strlen(errMsg1) < expMAX_MSG_LENGTH);
strcpy(Message, errMsg1);
}
else
{
assert((strlen(errMsg1) + strlen(errMsg2)) <
expMAX_MSG_LENGTH);
strcpy(Message, errMsg1);
strcat(Message, errMsg2);
}
}
virtual const char* what() const
{
return Message;
}
};
The reason I had two arguments to the constructor was to differentiate
the error from the location where it occured. For example, in the
following derived class:
class OrderOutOfRange: public Exception
{
public:
OrderOutOfRange(const char *eLoc = NULL)
: Exception("MyClass:: Order is out of Range ", eLoc) {}
};
and I can throw the errors as:
if (error) throw OrderOutOfRange("at MyConstructor(int, const char*)");
Is this a reasonable exception handling mechanism ? Are there better
ways to do the same thing ? (I don't want to start on the wrong foot,
especially with exception handling).
Thanks in advance,
Vijay.
I am an absolute newbie to Exception Handling, and I am trying to
retrofit exception handling to a LOT of C++ code that I've written
earlier. I am just looking for a bare-bones, low-tech exception
handling mechanism which will allow me to pass character information
about an error and its location from lower-level classes.
Can you please critique the following exception handling mechanism in
terms of my requirements ?
I have defined an Exception base class as follows:
#define expMAX_MSG_LENGTH 512
class Exception
{
char Message[expMAX_MSG_LENGTH];
public:
Exception()
{
strcpy(Message, "Exception !");
}
Exception(const char *errMsg1, const char *errMsg2 = NULL)
{
// I am using assert()'s here because it is truly a programming
// error to allow the string lengths to exceed 512 bytes.
if (errMsg2 == NULL)
{
assert(strlen(errMsg1) < expMAX_MSG_LENGTH);
strcpy(Message, errMsg1);
}
else
{
assert((strlen(errMsg1) + strlen(errMsg2)) <
expMAX_MSG_LENGTH);
strcpy(Message, errMsg1);
strcat(Message, errMsg2);
}
}
virtual const char* what() const
{
return Message;
}
};
The reason I had two arguments to the constructor was to differentiate
the error from the location where it occured. For example, in the
following derived class:
class OrderOutOfRange: public Exception
{
public:
OrderOutOfRange(const char *eLoc = NULL)
: Exception("MyClass:: Order is out of Range ", eLoc) {}
};
and I can throw the errors as:
if (error) throw OrderOutOfRange("at MyConstructor(int, const char*)");
Is this a reasonable exception handling mechanism ? Are there better
ways to do the same thing ? (I don't want to start on the wrong foot,
especially with exception handling).
Thanks in advance,
Vijay.