| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Boogie El Aceitoso" |
| Date: |
12 Nov 2003 07:51:15 AM |
| Object: |
Resume the computation after an exception |
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.
.
|
|
| User: "jeffc" |
|
| Title: Re: Resume the computation after an exception |
12 Nov 2003 03:29:03 PM |
|
|
"Boogie El Aceitoso" <frr149@telefonica.net> wrote in message
news:kle4rvoljel8avaienmbop6bsipbuftivh@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!
.
|
|
|
|
| User: "red floyd" |
|
| Title: Re: Resume the computation after an exception |
12 Nov 2003 11:25:21 PM |
|
|
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.
.
|
|
|
| User: "Kevin Saff" |
|
| Title: Re: Resume the computation after an exception |
20 Nov 2003 02:03:46 PM |
|
|
"red floyd" <no.spam@here.dude> wrote in message
news:5BEsb.13100$IL6.728@newssvr25.news.prodigy.com...
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.
.
|
|
|
| User: "red floyd" |
|
| Title: Re: Resume the computation after an exception |
20 Nov 2003 02:35:45 PM |
|
|
Kevin Saff wrote:
"red floyd" <no.spam@here.dude> wrote in message
news:5BEsb.13100$IL6.728@newssvr25.news.prodigy.com...
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.
.
|
|
|
|
|
|
| User: "Catalin Pitis" |
|
| Title: Re: Resume the computation after an exception |
12 Nov 2003 08:28:42 AM |
|
|
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" <frr149@telefonica.net> wrote in message
news:kle4rvoljel8avaienmbop6bsipbuftivh@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.
.
|
|
|
|
| User: "tom_usenet" |
|
| Title: Re: Resume the computation after an exception |
12 Nov 2003 09:20:37 AM |
|
|
On Wed, 12 Nov 2003 14:51:15 +0100, Boogie El Aceitoso
<frr149@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
.
|
|
|
|

|
Related Articles |
|
|