By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,384 Members | 2,030 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,384 IT Pros & Developers. It's quick & easy.

Exception Handling Critique ...

P: n/a
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.

--
PS(flame-guard): My id "Master Of C++" has more to do with my favorite
album "Master of Puppets" than with my proficiency in C++.

Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Master of C++ wrote:
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.

--
PS(flame-guard): My id "Master Of C++" has more to do with my favorite album "Master of Puppets" than with my proficiency in C++.


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

Jul 23 '05 #2

P: n/a
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.

Jul 23 '05 #3

P: n/a

Master of C++ wrote:
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

Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.