472,778 Members | 2,394 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,778 software developers and data experts.

Exception Handling Critique ...

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
3 2653
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: adi | last post by:
Dear all, This is more like a theoretical or conceptual question: which is better, using exception or return code for a .NET component? I had created a COM object (using VB6), which uses...
6
by: Daniel Wilson | last post by:
I am having exception-handling and stability problems with .NET. I will have a block of managed code inside try...catch and will still get a generic ..NET exception box that will tell me which...
7
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
2
by: tom | last post by:
Hi, I am developing a WinForm application and I am looking for a guide on where to place Exception Handling. My application is designed into three tiers UI, Business Objects, and Data Access...
9
by: C# Learner | last post by:
Some time ago, I remember reading a discussion about the strengths and weaknesses of exception handling. One of the weaknesses that was put forward was that exception handling is inefficient (in...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
4
by: Ele | last post by:
When Exception handling disabled compiler still spits out "C++ exception handler used." Why is that? Why does it ask for "Specify /EHsc"? Thanks! c:\Program Files\Microsoft Visual Studio...
41
by: Zytan | last post by:
Ok something simple like int.Parse(string) can throw these exceptions: ArgumentNullException, FormatException, OverflowException I don't want my program to just crash on an exception, so I must...
1
by: George2 | last post by:
Hello everyone, Such code segment is used to check whether function call or exception- handling mechanism runs out of memory first (written by Bjarne), void perverted() { try{
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.