try/catch and loop

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • King W.Wang

    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
  • Johnny

    #2
    Re: try/catch and loop

    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" <king_wwang@yah oo.de> wrote in message
    news:51d6351d.0 308060845.7dc61 d00@posting.goo gle.com...[color=blue]
    > Hi all,
    > is it possible or is it good practice to enclose try/catch in a loop, as:
    >
    > for (...) {
    > try {
    > ......
    > }
    > catch (...) {
    > ......
    > }
    > }
    >
    > Many thanks
    >
    > Weichao[/color]


    Comment

    • john bailo

      #3
      Re: try/catch and loop

      Johnny:
      [color=blue]
      > 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.[/color]

      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
      Trực tiếp bóng đá hôm nay tại CakhiaTV, Link xem Cakhia TV ổn định không lag giật, chất lượng cao. Bình luận tiếng Việt miễn phí, hình ảnh sắc nét tại Cà Khịa TV.


      Comment

      • cvissy

        #4
        Re: try/catch and loop

        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
        ClassNotFoundEx ception 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.

        Comment

        • john bailo

          #5
          Re: try/catch and loop

          cvissy:
          [color=blue]
          > 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
          > ClassNotFoundEx ception 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.[/color]

          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.



          --

          geeks for dean '04

          Comment

          Working...