On 2007-12-16 14:10:50 -0500, Erik Wikström <Er***********@telia.comsaid:
>
bool succeed = false;
while (!succeed)
{
try
{
doSomelthingThatMightThrow()
succeed = true;
}
catch(SomeException& e)
{
logFailure();
}
}
A bit off topic, but must you utilize the flag variable succeed? That
approach feels so C-like.
Rather, how about the following version?
while(1)
try
{
doSomething();
break;
}
catch(SomeException &e)
{
fixProblem();
}
>
In other cases you might try a different approach:
int result;
try
{
result = firstApproach();
}
catch(Exception& e)
{
result = secondApproach();
}
Err... For multiple approaches, I rather recommend the following
more-symmetrical structure instead:
for(;;)
{
try { approach1(); break; }
catch(SomeException &e) { logFailure(); }
try { approach2(); break; }
catch(SomeException &e) { logFailure(); }
try { approach3(); break; }
catch(SomeException &e) { logFailure(); }
throw AllAproachFailedException();
}
--
-kira