473,326 Members | 2,337 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,326 software developers and data experts.

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 14175
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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) {}
9
by: Mark | last post by:
Hello I'm trying to use a Server.Transfer in a try-catch (I cannot put it outside the Try-Catch as it is nested deep within a component that is called in a try-catch loop) The problem is that the...
5
by: Doug Gifford | last post by:
WROX "Beginning VB.Net" 2nd Edition Using this code from Chapter 11 Page 472: Do Try ' Read a line from the file... currentLine = myReader.ReadLine lineCounter = lineCounter + 1 Catch e As...
2
by: Keith Kowalski | last post by:
I anm opening up a text file reading the lines of the file that refer to a tif image in that file, If the tif image does not exist I need it to send an email stating that the file doesn't exist...
6
by: ECathell | last post by:
I am having a problem with my try..catch blocks... I push a button on my form. using SQLDMO I verify the status of the server. If it is off I turn it on. I then run a query that requires a...
8
by: Luke Davis | last post by:
I'm new so bear with me. Is there a way for the catch to fix a problem then start the try over again? For example, I have this console application sync a remote database and it takes hours. ...
3
by: =?Utf-8?B?SmVycnk=?= | last post by:
Hi All, I want to fill some textbox with properties from Active Directory. The fact is if the property is unavailable the program will crash. You can prevent this by using a try and catch. So...
4
by: =?Utf-8?B?UmljaA==?= | last post by:
I have to read data from an Excel Sheet. Using Microsoft Office Interop and Automation I create an Excel object Dim xl As New Excel.Application, wkbk As Excel.Workbook, rng as Excel.Range I...
16
by: Andy B | last post by:
I have the following code inside of a WebBrowser.DocumentCompleted event: For index As Integer = 0 To Me.Browser.Document.GetElementsByTagName("ul").Item(0).GetElementsByTagName("li").Count ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.