Nested try/catch blocks to give 2 attempts on an operation

B

beta2000

I want to try Refresh() again if the first Refresh() fails. Code:

try
{
try
{
Refresh(); // ... first attempt...
}
catch(...)
{
Refresh(); // ... second attempt...
}
}
catch(...)
{
// Display error message (both attempts failed)
}

My question is -- Is this a good style? Is there a way to not use so
many try/catch and still attempt the operation up to 2 times?
 
V

Victor Bazarov

I want to try Refresh() again if the first Refresh() fails. Code:

try
{
try
{
Refresh(); // ... first attempt...
}
catch(...)
{
Refresh(); // ... second attempt...
}
}
catch(...)
{
// Display error message (both attempts failed)
}

My question is -- Is this a good style? Is there a way to not use so
many try/catch and still attempt the operation up to 2 times?

I think it would be better to have a loop

bool go_on = true;
int attempt = 0, total = 2;

while (go_on) {
try {
Refresh();
go_on = false; // if manages to get here, no need to repeat
}
catch(...) { // whatever you catch
go_on = ++attempt < total;
if (go_on)
do_something_to_set_up_another_attempt(attempt);
}
}

if (attempt == total)
// all attempts failed

That way you can control the number of attempts much easier.

V
 
R

red floyd

I want to try Refresh() again if the first Refresh() fails. Code:

try
{
try
{
Refresh(); // ... first attempt...
}
catch(...)
{
Refresh(); // ... second attempt...
}
}
catch(...)
{
// Display error message (both attempts failed)
}

My question is -- Is this a good style? Is there a way to not use so
many try/catch and still attempt the operation up to 2 times?

You could use a loop. That also allows you easily change the number of
tries.

const int tries = 2;
int j; // deliberately outside the loop
for (j = 0; j < tries ; ++j)
{
try
{
Refresh();
break;
}
catch(...)
{
/* do nothing */
}
}

if (j >= tries)
{
// have a panic attack, all tries failed
}
 
A

Alf P. Steinbach

* (e-mail address removed):
I want to try Refresh() again if the first Refresh() fails. Code:

try
{
try
{
Refresh(); // ... first attempt...
}
catch(...)
{
Refresh(); // ... second attempt...
}
}
catch(...)
{
// Display error message (both attempts failed)
}

My question is -- Is this a good style?

Not in C++.

Is there a way to not use so many try/catch and still attempt
the operation up to 2 times?

void RefreshWithRetry()
{
try{ Refresh(); }
catch( std::exception const& ) { Refresh(); }
}

....
try
{
RefreshWithRetry();
}
catch( std::exception const& x )
{
// Display error message (both attempts failed)
}

If you want a more general solution you could use a loop, and if you want a
still more general solution you could generalize that to a class with the
action performed by a virtual function.
 
B

beta2000

red said:
You could use a loop. That also allows you easily change the number of
tries.

const int tries = 2;
int j; // deliberately outside the loop
for (j = 0; j < tries ; ++j)
{
try
{
Refresh();
break;
}
catch(...)
{
/* do nothing */
}
}

if (j >= tries)
{
// have a panic attack, all tries failed
}

Building on this idea, I did something similar (which uses one less
variable :p)...

int tries = 2;
while ( tries > 0 )
{
try
{
Refresh();
break;
}
catch(...)
{
--tries;
}
}

if ( tries == 0 )
ShowMessage("Error");
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top