design a C++ class for fixing errors

V

vadi

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?
 
D

David Harmon

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"
 
L

lilburne

??????? ?????? said:
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.
 
J

Jeff Schwab

lilburne said:
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.
 
L

lilburne

Jeff said:
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:

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

Roman Gavrilov

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 ErrorUserIsReallyDumb : 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(ErrorUserIsDumb());
else throw (ErrorUserIsReallyDumb());
}
}
catch(IError & e) {
e.Recover();
bTryAgain = true;
}

} while (bTryAgain);

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

return 0;
}
 
J

Jorge Rivera

vadi said:
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(probably 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.insert(std::pair<int, $(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
 

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

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top