By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,419 Members | 1,123 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,419 IT Pros & Developers. It's quick & easy.

Resume the computation after an exception

P: n/a

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.
Jul 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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

"Boogie El Aceitoso" <fr****@telefonica.net> wrote in message
news:kl********************************@4ax.com...

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.

Jul 19 '05 #2

P: n/a
On Wed, 12 Nov 2003 14:51:15 +0100, Boogie El Aceitoso
<fr****@telefonica.net> wrote:

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
Jul 19 '05 #3

P: n/a

"Boogie El Aceitoso" <fr****@telefonica.net> wrote in message
news:kl********************************@4ax.com...

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!
Jul 19 '05 #4

P: n/a
Boogie El Aceitoso wrote:
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.

Jul 19 '05 #5

P: n/a
"red floyd" <no*****@here.dude> wrote in message
news:5B*****************@newssvr25.news.prodigy.co m...
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.

Jul 22 '05 #6

P: n/a
Kevin Saff wrote:
"red floyd" <no*****@here.dude> wrote in message
news:5B*****************@newssvr25.news.prodigy.co m...
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.


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.
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.