Exception Handling Critique ...

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.
 
Z

zeotherm

Master said:
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.

The basic layout seems okay, it is similar to what I do. However, why
are you using char arrays instead of std::string, the same goes for
strcpy and strcat... Unless you absolutely need these, use the string
class. Also note that exception handling can be quite slow, so don't
use it in a tight loop.

- ZT
 
M

Master of C++

ZT,

Thanks for your response.

The reason I preferred char arrays to strings is that, strings require
dynamic memory and can also throw() and that, in my current opinion,
may not be a good thing (please correct me if I am wrong). I wanted the
Exception class to be as low-tech as possible so that nothing throws up
from within the Exception classes. (I have fortified the Exception
classes with asserts() to check the message lengths)

-Vijay.
 
Z

zeotherm

Master said:
ZT,

Thanks for your response.

The reason I preferred char arrays to strings is that, strings require
dynamic memory and can also throw() and that, in my current opinion,
may not be a good thing (please correct me if I am wrong). I wanted the
Exception class to be as low-tech as possible so that nothing throws up
from within the Exception classes. (I have fortified the Exception
classes with asserts() to check the message lengths)

-Vijay.

Vijay, if string throws an out of memory exception (which would be
thrown when creating string) what would you do with it? There is no
memory to work with and your program is screwed. While in the end it
is your choice, and your design, I think you are asking for more
trouble using char arrays as oppsed to strings. If you can GUARANTEE
that no message will ever be greater then 512 in length and you are not
at all worried about using strcmp and strcat, then go ahead. However,
I think in the general case that std::string will make you happier in
the long run. Just my $0.02. Again, I think the design is fine. Good
luck with!

- ZT
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top