Resume the computation after an exception

B

Boogie El Aceitoso

Hi,

Is it possible to 'fix'in the catch block whatever caused an exception and
return to the offending statement in the try block, and try it again?

After raising an exception, I'd like to try to resume the computation after
fixing whatever went wrong.
 
C

Catalin Pitis

I don't know all the details of the code you wrote or intend to write.

If you know which statement can cause the problem (throw the exception),
then you can enclose that statement in a separate try-catch block. The catch
block will handle the problem, then the execution will continue with the
next statement.

Catalin
 
T

tom_usenet

Hi,

Is it possible to 'fix'in the catch block whatever caused an exception and
return to the offending statement in the try block, and try it again?

You can't.

The book "The Design and Evolution of C++" by Bjarne Stroustrup
explains why there isn't a way to do this (you can do something
slightly similar in some languages I think, VB for one, although I
can't imagine anyone wanting to copy VB's error handling model!).
After raising an exception, I'd like to try to resume the computation after
fixing whatever went wrong.

It may be that exceptions aren't what you should be using here. Why
not handle the error where it happens, and then continue if you can?
Why are you letting the exception propogate out of the operation?

Tom
 
J

jeffc

Boogie El Aceitoso said:
Hi,

Is it possible to 'fix'in the catch block whatever caused an exception and
return to the offending statement in the try block, and try it again?

After raising an exception, I'd like to try to resume the computation after
fixing whatever went wrong.

No. Basically, exceptions were designed for cases where you *can't* fix it
and continue - at least not on the same path. If you have such a case, then
supposedly you shouldn't throw an exception, but deal with it in some other
way. It's like a function is saying "it's not possible", and you're
replying "try harder". If he *could* try harder, he should have done so
before throwing an exception!
 
R

red floyd

Boogie said:
Hi,

Is it possible to 'fix'in the catch block whatever caused an exception and
return to the offending statement in the try block, and try it again?

After raising an exception, I'd like to try to resume the computation after
fixing whatever went wrong.

No. C++ exceptions use the Ada model.

If you really want to do fix it, you could try this:

for (;;)
{
try
{
// YOUR EXCEPTION PRONE CODE HERE!!!!
break;
}
catch (some_exception_type& ex)
{
// fix something
}
catch (some_other_exception_type& ex)
{
// fix something
}
// etc...
}

This will keep trying until you either get something that works, or give up and throw an exception that you don't look for in your
catch blocks.
 
K

Kevin Saff

red floyd said:
No. C++ exceptions use the Ada model.

If you really want to do fix it, you could try this:

for (;;)
{
try
{
// YOUR EXCEPTION PRONE CODE HERE!!!!
break;
}
catch (some_exception_type& ex)
{
// fix something
}
catch (some_other_exception_type& ex)
{
// fix something
}
// etc...
}

This will keep trying until you either get something that works, or give
up and throw an exception that you don't look for in your
catch blocks.

If you are not very careful it will just repeat the same code over and over
again, trying the same fix each time. I think it is hard to see a good
general way to avoid that problem, so I prefer wrapping the repeated code in
a function, and re-calling that function in the catch blocks if the problem
looks fixable.
 
R

red floyd

Kevin said:
up and throw an exception that you don't look for in your



If you are not very careful it will just repeat the same code over and over
again, trying the same fix each time. I think it is hard to see a good
general way to avoid that problem, so I prefer wrapping the repeated code in
a function, and re-calling that function in the catch blocks if the problem
looks fixable.

You're correct. I wasn't clear in my earlier post. That's what the "give up" comment was about.
Sooner or later, you need to say, "enough is enough", toss in your cards, and throw an exception that you deliberately don't catch.
 

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,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top