473,772 Members | 2,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

design a C++ class for fixing errors

I need to design a class for fixing different kind of errors. The
errrors fall into number of categories. For each category the
methodology of fixing the error
is different. I do not want to use a switch...case statement for
handling different category of errors. Can any one suggest me the
method?
Jul 22 '05 #1
7 1633
On 7 Feb 2004 06:28:55 -0800 in comp.lang.c++, va********@yaho o.com
(vadi) was alleged to have written:
methodology of fixing the error
is different. I do not want to use a switch...case statement for
handling different category of errors.


Please read recent thread "Subject: Re: Need help with switch() mess"

Jul 22 '05 #2

You can use construction
TRY
THROW
CATCH
"vadi" <va********@yah oo.com> ???????/???????? ? ???????? ?????????:
news:1c******** *************** ***@posting.goo gle.com...
I need to design a class for fixing different kind of errors. The
errrors fall into number of categories. For each category the
methodology of fixing the error
is different. I do not want to use a switch...case statement for
handling different category of errors. Can any one suggest me the
method?

Jul 22 '05 #3
??????? ?????? wrote:
You can use construction
TRY
THROW
CATCH


How does that help?

catch (errortype1) {
}
catch (errortype2) {
}
catch (errortypeN) {
}

is just a switch under a different name. Except that given
the throw he will have lost the context of the error's location.
Jul 22 '05 #4
lilburne wrote:
??????? ?????? wrote:
You can use construction
TRY
THROW
CATCH

How does that help?

catch (errortype1) {
}
catch (errortype2) {
}
catch (errortypeN) {
}

is just a switch under a different name. Except that given the throw he
will have lost the context of the error's location.

You're dead wrong. Try/catch is definitely the way to go here.

Jul 22 '05 #5
Jeff Schwab wrote:
lilburne wrote:
??????? ?????? wrote:
You can use construction
TRY
THROW
CATCH


How does that help?

catch (errortype1) {
}
catch (errortype2) {
}
catch (errortypeN) {
}

is just a switch under a different name. Except that given the throw
he will have lost the context of the error's location.


You're dead wrong. Try/catch is definitely the way to go here.


Using try/catch the catch has to be able to distinguish
between the different types that can be thrown - yes.

The OP also says he doesn't want to use a switch statement and:
catch (errortype1) {
}
catch (errortype2) {
}
catch (errortypeN) {
}


looks like a switch or multiway if statement.

The OP also says "I need to design a class for fixing
different kind of errors. The errrors fall into number of
categories. For each category the methodology of fixing the
error is different.". What he wants is a hierarchy of error
handling classes derived from some ABC.

Whether he then decides to throw an instance of the
hierarchy, or process the error in situ is independent of
how he arranges his error handling class to avoid a switch
statement.

Jul 22 '05 #6
va********@yaho o.com (vadi) wrote in message news:<1c******* *************** ****@posting.go ogle.com>...
I need to design a class for fixing different kind of errors. The
errrors fall into number of categories. For each category the
methodology of fixing the error
is different. I do not want to use a switch...case statement for
handling different category of errors. Can any one suggest me the
method?


I think the polymorphism is the solution designed to replace switch
mess.
The idea is to implement the same interface differently, and throw
different exception objects.

Hope this helps,
regards,
Roman

--- here goes snippet ---

#include <iostream>

using namespace std;

class IError {
public:
virtual void Recover(void) = 0;
};

class ErrorUserIsDumb : public IError {
public:
void Recover(void) {
// do a recovery
cout << "User, you are dumb." << endl;
}
};
class ErrorUserIsReal lyDumb : public IError {
public:
void Recover(void) {
// do a recovery
cout << "User, you are really dumb." << endl;
}
};
int main(int argc, char* argv[])
{
int i;
bool bTryAgain;
int cntTry = 0;

do {

bTryAgain = false;
++cntTry;
try {
cout << "Enter value other then 1: ";
cin >> i;

if(i == 1) {
if(cntTry == 1) throw(ErrorUser IsDumb());
else throw (ErrorUserIsRea llyDumb());
}
}
catch(IError & e) {
e.Recover();
bTryAgain = true;
}

} while (bTryAgain);

cout << "Value is " << i << endl;

return 0;
}
Jul 22 '05 #7
vadi wrote:
I need to design a class for fixing different kind of errors. The
errrors fall into number of categories. For each category the
methodology of fixing the error
is different. I do not want to use a switch...case statement for
handling different category of errors. Can any one suggest me the
method?


Assuming that you assign a status to each error, I think that the
fastestt lookup time for an error is through something like a hashed map.

I don't remember the syntax of the STL like hash_map, but in general it
should be something in the line of: hash_map<type K, type V>. The
constructor probably asks you to provide a hashing function (or it will
give you a default one. Having said this, in order to so this approach,
the only way to execut code after you do a lookup is by having a
function pointer returned, therefore this is complex, troublesome and a
nightmare to maintain.

However, I do beleive that well-implemented, this would yield a superior
performance to switch.

To summarize, you would probably end up writing something that *MIGHT*
end up looking like this:

// I'm sure someone in this newsgroup knows of a good struct to use tat
// encapsulates functors(probab ly something like
// boost::function ::<something>

// This handles errors of one type
struct handle1 : $(some_functor) {
void operator(){
// handle error of sometype
};
};
hash_map<int, $(some_functor) > hashed_map;

// Initialize somewhere
hashed_map.inse rt(std::pair<in t, $(some_functor) >(error1, handle1));
.....
// Then to run
hashed_map[error1]();
I know this will be far from compiling. This is just an idea...
You might also find something with template metaprogramming , but I don't
know much about this.

Regards,

JLR
Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

36
6401
by: Andrea Griffini | last post by:
I did it. I proposed python as the main language for our next CAD/CAM software because I think that it has all the potential needed for it. I'm not sure yet if the decision will get through, but something I'll need in this case is some experience-based set of rules about how to use python in this context. For example... is defining readonly attributes in classes worth the hassle ? Does duck-typing scale well in complex
13
2105
by: KV | last post by:
I'm new to OO Design, and I'm fixing to start writing my very first C# program. Given the complexity of OO programming, I would like to run something by this group and get general input. My example is a program called HijackThis. I'm sure many are familiar that it is a spyware removal tool. The program looks at over 20 places on Windows computers to see what is starting (with options to remove the offending software). The program can...
11
2400
by: DrNoose | last post by:
Hi! I've got a program that's almost done, but I'm getting compile errors in two lines: 317 & 319. I think the main error has to do with the Truck Class. I'm a newbie and keep looking at the code but can't seem to see the error. I don't understand a lot of the fancy stuff so if you can keep it simple for me!!!! Any help is appreciated! Here is the code:
3
1614
by: pauldepstein | last post by:
Sorry in advance if this message sounds imprecise but it's difficult to be precise when you don't really understand what's going on. I have a class called Parameters. The default constructor Parameter:Parameter() contains various values such as interest = 0.05; My intent is for the user to be able to override these defaults so I have a function void Parameter::set(void) . This is supposed to give the user a chance
3
1648
by: Luis P. Mendes | last post by:
Hi, I have the following problem: I instantiate class Sistema from another class. The result is the same if I import it to interactive shell. s = Sistema("par") class Sistema:
4
1734
by: rhaazy | last post by:
I have created this very primitive class for the purpose of performing some simple recursive functions. I am new to C++ and the concept of classes all together. I put this together using various resources on the net, but am faced with an insane amount of compiler errors I don't know what to do with. Any help would be greatly appreciated. #include <iostream> using namespace std;
11
3001
by: Rik | last post by:
Hello guys, now that I'm that I'm working on my first major 'open' forms (with uncontrolled users I mean, not a secure backend-interface), I'd like to add a lot of possibilities to check wether certain fields match certain criteria, and inform the user in different ways when the data is wrong (offcourse, this will be checked on posting the data again, but that's something I've got a lot of experience with). Now, offcourse it's...
18
25006
by: Diogenes | last post by:
Hi All; I, like others, have been frustrated with designing forms that look and flow the same in both IE and Firefox. They simply did not scale the same. I have discovered, to my chagrin, that IE7 does not seem to offer any way to control the font size of a text input element.
6
1712
by: dragoncoder | last post by:
Hello experts, This is actually a design problem which I want to solve using c++. I have the following class. class Animal { public: virtual void run() { uselegs(); }
0
9619
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9911
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.