Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old February 27th, 2007, 03:55 AM
Gary Wessle
Guest
 
Posts: n/a
Default try and catch

Hi

I am using exceptions in my code and not sure how things are suppose
to work in my case.

while (1){
try
{
// call some methods here and use their side effect and returns.
break;
}
catch ( catch'm )
{
// something wicked happened.
}
}

Is this ok?
I mean, I keep on trying to do the task and break once it is done or
handle the error and get back to the same task again in case a throw
happens?

thanks
  #2  
Old February 27th, 2007, 04:25 AM
Alf P. Steinbach
Guest
 
Posts: n/a
Default Re: try and catch

* Gary Wessle:
Quote:
Hi
>
I am using exceptions in my code and not sure how things are suppose
to work in my case.
>
while (1){
try
{
// call some methods here and use their side effect and returns.
break;
}
catch ( catch'm )
{
// something wicked happened.
}
}
>
Is this ok?
Well, it's a well-known pattern, but at the lowest possible level of
abstraction.

Note: if you don't use a counter or other loop variant, you risk an
infinite loop.

Also, to avoid silly-warnings from some compilers, better write
'for(;;)' than 'while(1)', even if the latter is an old C idiom.

Quote:
I mean, I keep on trying to do the task and break once it is done or
handle the error and get back to the same task again in case a throw
happens?
Yeah, OK.

But if you have that low-level pattern occurring more than one place in
your code, consider abstracting it using the template pattern (nothing
to do with templates), like

const unsigned untilDoomsday = unsigned(-1);

struct Retry
{
bool operator( unsigned maxTries ) const
{
while( maxTries 0 )
{
try
{
doIt();
return true;
}
catch( std::exception const& x )
{
onException( x );
}
if( maxTries != untilDoomsday ) { --maxTries; }
}
return false;
}

virtual void doIt() const = 0;
virtual void onException( std::exception const& x ) const {}
};

...
const bool succeeded = RetryDangerousThing()( untilDoomsday );

IIRC this was John Harrison's idea (not his code though, the above is
just scribbled down, guaranteed untouched by compiler's hands).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
  #3  
Old March 3rd, 2007, 04:05 PM
Rolf Magnus
Guest
 
Posts: n/a
Default Re: try and catch

Alf P. Steinbach wrote:
Quote:
But if you have that low-level pattern occurring more than one place in
your code, consider abstracting it using the template pattern (nothing
to do with templates), like
Well, it could be combined with templates. I don't really see a need for
runtime polymorphism here.
Quote:
const unsigned untilDoomsday = unsigned(-1);
>
struct Retry
{
bool operator( unsigned maxTries ) const
ITYM:

bool operator()( unsigned maxTries ) const

Quote:
{
while( maxTries 0 )
{
try
{
doIt();
return true;
}
catch( std::exception const& x )
{
onException( x );
}
if( maxTries != untilDoomsday ) { --maxTries; }
}
return false;
}
>
virtual void doIt() const = 0;
virtual void onException( std::exception const& x ) const {}
};
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles