469,271 Members | 1,009 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

try/catch and loop

Hi all,
is it possible or is it good practice to enclose try/catch in a loop, as:

for (...) {
try {
......
}
catch (...) {
......
}
}

Many thanks

Weichao
Jul 17 '05 #1
4 13921
It's certainly possible but only sensible if you want to do something in a
loop. If this is the case you'd usually be better off putting the try/catch
block outside the loop.

"King W.Wang" <ki********@yahoo.de> wrote in message
news:51**************************@posting.google.c om...
Hi all,
is it possible or is it good practice to enclose try/catch in a loop, as:

for (...) {
try {
......
}
catch (...) {
......
}
}

Many thanks

Weichao

Jul 17 '05 #2
Johnny:
It's certainly possible but only sensible if you want to do something in a
loop. If this is the case you'd usually be better off putting the try/catch
block outside the loop.


ok, but here's another question. can you put it
in a loop AND set the loop condition based on
catching an error -- that is, say i want to 'retry'
and operation 3 times IFF it throws an error. can
i use the results of the catch as an evaluator in
the while loop

string e = "";
int count = 0;
while (e=="") and (count<3){
try {
//connect to database
}
catch (Exception eq) {
e=eq.ToString();
count+=1;
}
}

--
dean '04
http://deanforamerica.com

Jul 17 '05 #3
As far as I know you can do something like that, but its not a good
idea to base your program flows on exceptions.

Rather have your program flow well structured to catch your exceptions
and deal with them immediately.

The example you gave leaves you open to a few problems because you are
making a general catch to all exceptions - maybe a
ClassNotFoundException occurs and you want to tell the user that they
can find the driver file, or a SQLException occurs and you must inform
the user that there is a problem with the JDBC:ODBC bridge. Neither of
these situations would be helped by simply catching a generic and
looping.
Jul 17 '05 #4
cvissy:
As far as I know you can do something like that, but its not a good
idea to base your program flows on exceptions.

Rather have your program flow well structured to catch your exceptions
and deal with them immediately.

The example you gave leaves you open to a few problems because you are
making a general catch to all exceptions - maybe a
ClassNotFoundException occurs and you want to tell the user that they
can find the driver file, or a SQLException occurs and you must inform
the user that there is a problem with the JDBC:ODBC bridge. Neither of
these situations would be helped by simply catching a generic and
looping.


excellent advice. i agree with you. AP in ANG suggested a do loop, that
lets the catch increment ( counter++ ) and runs 3 tries. i like your idea
that the catch should be more discriminating and so i could build a
catch(exception e) case statement that only increments the loop for
specific conditions, or does a break; if there are conditions under which
the jdbc connect should not be retried.

--
http://geeks4dean.com
geeks for dean '04

Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Pohihihi | last post: by
9 posts views Thread by Mark | last post: by
5 posts views Thread by Doug Gifford | last post: by
8 posts views Thread by Luke Davis | last post: by
3 posts views Thread by =?Utf-8?B?SmVycnk=?= | last post: by
16 posts views Thread by Andy B | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.