none
try-catch RRS feed

  • Question

  • Is there any way with a try-catch that if the try fails that I retry it?

    Basically I have an app that downloads text files, and then processes them. However, at my location we lose internet connectivity on a fairly common basis, and I don't want the app to try to process half downloaded files.

    The downloading sequence is in a class by itself, so I just basically want to do the following:

    try
    {
        download files
    }
    catch
    {
        try again
    }

    I haven't found any way to do it so far, so any help would be appreciated.

    Thanks,
    Russ

    Friday, October 27, 2006 7:34 PM

Answers

  • the example shows you how :-P

    If you could have one method such as "DoTrySomething(bool keepTrying)" then in here have your try catch block, it will keep calling itself until you set the keepTrying bool parameter to false. Example:

    //global

    private int maxNumberOfTries = 3;

    private int numberOfTries = 0;

    ...

    ...

    this.DoTrySomething(true);

    ..

     

    private void DoTrySomething(bool keepTrying)

    {

       if (!keepTrying || this.maxNumberOfTries < numberOfTries)

       {

          try { .... }

          catch(Exception e)

          {

            this.numberOfTries++; 

            this.DoTrySomething(true)

          }

       }

    }

     

    so here, this method will keep calling itself when there has been an error unless either the parameter passed in (keepTrying) is false OR the numberOfTries equals the maxNumberOfTries and increments the numberOfTries by one on every exception that it catches

    does this make more sense?

    the idea is:

  • keep calling yourself

  • if catch exception -> increment the number of times tried and call yourself

  • if numberOfTries == maxNumberOfTries or you don't want to call yourself again, call yourself with "false".

     

    I guess you could do away with the bool value and just keep an eye on the maxNumberOfTries. Example:

     

    //global

    private int maxNumberOfTries = 3;

    private int numberOfTries = 0;

    ...

    ...

    this.DoTrySomething(true);

    ..

     

    private void DoTrySomething(int triesHad)

    {

       if (triesHad < this.maxNumberOfTries)

       {

          try { .... }

          catch(Exception e)

          {

            this.numberOfTries++; 

            this.DoTrySomething(this.numberOfTries)

          }

       }

    }

     

    does this make sense?

Friday, October 27, 2006 8:22 PM
Moderator
  • Hi,

    you could also use a straight forward loop. That would also avoid stack overflows because of deep recursions:

    void foo()
    {
      bool tryAgain = true;

      while(tryAgain)
      {
        tryAgain = false;
        try
        {
          // do some work which may throw SomeException
        }
        catch(SomeException ex)
        {
          // clean up if you can
          tryAgain = true;
        }
      }
    }

    --
    SvenC

    Friday, October 27, 2006 8:31 PM
  • All replies

    • there isn't a way but you can try to recall itself, the method rather, which has the try catch block to do whatever it is you are doing. (Recursive method). You could also then set some "numberOfRetries" variable globally so everytime you catch an exception it would add 1 to it but if it gets to 3, then don't recall the method.

      take a look at this too:

      http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=808935&SiteID=1

      Friday, October 27, 2006 7:45 PM
      Moderator
    • I'm not very clear on how to use the recursive method from this post or the one that you gave the link to. Could you possibly show me how it would be used?

      Thanks,
      Russ

      Friday, October 27, 2006 8:08 PM
    • the example shows you how :-P

      If you could have one method such as "DoTrySomething(bool keepTrying)" then in here have your try catch block, it will keep calling itself until you set the keepTrying bool parameter to false. Example:

      //global

      private int maxNumberOfTries = 3;

      private int numberOfTries = 0;

      ...

      ...

      this.DoTrySomething(true);

      ..

       

      private void DoTrySomething(bool keepTrying)

      {

         if (!keepTrying || this.maxNumberOfTries < numberOfTries)

         {

            try { .... }

            catch(Exception e)

            {

              this.numberOfTries++; 

              this.DoTrySomething(true)

            }

         }

      }

       

      so here, this method will keep calling itself when there has been an error unless either the parameter passed in (keepTrying) is false OR the numberOfTries equals the maxNumberOfTries and increments the numberOfTries by one on every exception that it catches

      does this make more sense?

      the idea is:

    • keep calling yourself

    • if catch exception -> increment the number of times tried and call yourself

    • if numberOfTries == maxNumberOfTries or you don't want to call yourself again, call yourself with "false".

       

      I guess you could do away with the bool value and just keep an eye on the maxNumberOfTries. Example:

       

      //global

      private int maxNumberOfTries = 3;

      private int numberOfTries = 0;

      ...

      ...

      this.DoTrySomething(true);

      ..

       

      private void DoTrySomething(int triesHad)

      {

         if (triesHad < this.maxNumberOfTries)

         {

            try { .... }

            catch(Exception e)

            {

              this.numberOfTries++; 

              this.DoTrySomething(this.numberOfTries)

            }

         }

      }

       

      does this make sense?

    Friday, October 27, 2006 8:22 PM
    Moderator
  • Yes, that makes perfect sense. I have one more question if you don't mind.

    If this is my method:

    public void wp_download_process(object sender, DoWorkEventArgs e)
    {
    }

    How do I call it?

    I am trying to use:

    this.wp_download_process("I don't know what goes here", e.Argument);

    And keep getting errors.

    Thanks for all your help,
    Russ

    Friday, October 27, 2006 8:26 PM
  • Hi,

    you could also use a straight forward loop. That would also avoid stack overflows because of deep recursions:

    void foo()
    {
      bool tryAgain = true;

      while(tryAgain)
      {
        tryAgain = false;
        try
        {
          // do some work which may throw SomeException
        }
        catch(SomeException ex)
        {
          // clean up if you can
          tryAgain = true;
        }
      }
    }

    --
    SvenC

    Friday, October 27, 2006 8:31 PM
  • good point Sven, I knew there had to be a simpler way! however even using the previous one wouldnt have the stack overflow exception since you get a max of 3 tries and thats it.

    In regards to the other question, im not sure. Is this a backgroundworker thread?

    Friday, October 27, 2006 8:40 PM
    Moderator
  • Yes, this is a backgroundworker thread.

    Thanks,
    Russ

    Friday, October 27, 2006 8:45 PM
  • im not an expert on the backgroundworker thread to be honest so I guess a good place to start would be here:

    http://msdn2.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

    as there is tons of docs and examples. Sorry I couldnt be of much help on this one

    Friday, October 27, 2006 8:57 PM
    Moderator
  • I ended up going with Sven's model, and it worked great.

    Thanks a lot guys for all your help,
    Russ

    Wednesday, November 1, 2006 4:10 PM