473,388 Members | 1,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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
Feb 27 '07 #1
2 1891
* Gary Wessle:
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.

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?
Feb 27 '07 #2
Alf P. Steinbach wrote:
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.
const unsigned untilDoomsday = unsigned(-1);

struct Retry
{
bool operator( unsigned maxTries ) const
ITYM:

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 {}
};
Mar 3 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Gary.Hu | last post by:
I was trying to catch the Arithmetic exception, unsuccessfully. try{ int a = 0, b = 9; b = b / a; }catch(...){ cout << "arithmetic exception was catched!" << endl; } After ran the program,...
11
by: kaeli | last post by:
Hey all, I'd like to start using the try/catch construct in some scripts. Older browsers don't support this. What's the best way to test for support for this construct so it doesn't kill...
4
by: Abhishek Srivastava | last post by:
Hello All, I have seen code snippets like try { ..... } catch {
11
by: Pohihihi | last post by:
I was wondering what is the ill effect of using try catch in the code, both nested and simple big one. e.g. try { \\ whole app code goes here } catch (Exception ee) {}
13
by: Benny | last post by:
Hi, I have something like this: try { // some code } catch // note - i am catching everything now {
23
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally...
32
by: cj | last post by:
Another wish of mine. I wish there was a way in the Try Catch structure to say if there wasn't an error to do something. Like an else statement. Try Catch Else Finally. Also because I...
23
by: pigeonrandle | last post by:
Hi, Does this bit of code represent complete overkill?! try { //create a treenode TreeNode tn = new TreeNode(); //add it to a treeview tv.Nodes.Add(tn);
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.