Repeating a function in C# until it no longer throws an exception

后端 未结 11 2074
一生所求
一生所求 2021-01-18 00:31

I\'ve got a class that calls a SOAP interface, and gets an array of data back. However, if this request times out, it throws an exception. This is good. However, I want m

相关标签:
11条回答
  • 2021-01-18 00:38

    It its not gernally a good idead to use exceptions as control flow, but this will do what you requested.

    bool Caught = true;
    while (Caught)
    try
    {
        salesOrdersArray = MagServ.salesOrderList(sessID, filter);
        Caught = false;
    }
    catch
    {
        Caught = true;
    }
    
    0 讨论(0)
  • 2021-01-18 00:39

    You just need to loop forever:

    while (true)
    {
        try
        {
            salesOrdersArray = MagServ.salesOrderList(sessID, filter);
            break; // Exit the loop. Could return from the method, depending
                   // on what it does...
        }
        catch
        {
            // Log, I suspect...
        }
    }
    

    Note that you should almost certainly not actually loop forever. You should almost certainly have a maximum number of attempts, and probably only catch specific exceptions. Catching all exceptions forever could be appalling... imagine if salesOrderList (unconventional method name, btw) throws ArgumentNullException because you've got a bug and filter is null... do you really want to tie up 100% of your CPU forever?

    0 讨论(0)
  • 2021-01-18 00:39

    If you can't change the timeout, the below should work. salesOrdersArray should be initialized to null.

    while(salesOrdersArray == null)
    {
        try
        {
           salesOrdersArray = MagServ.salesOrderList(sessID, filter);
        }
        catch
        {
           // Log failure
        }
    }
    
    0 讨论(0)
  • 2021-01-18 00:40

    You must place the try/catch block inside a loop construct. If you wish not to consume 100% of your processor place a Thread.Sleep in the catch block, so everytime an exception occurs, it will wait some time, freeing the processor to do other things.

    // iterate 100 times... not forever!
    for (int i = 0; i < 100; i++)
    {
        try {
            // do your work here;
    
            break; // break the loop if everything is fine
        } catch {
            Thread.Sleep(1000);
        }
    }
    

    You could also specify exception type, so that only the timeout exception is handled, and other kinds of exceptions pass-through.

    // iterate 100 times... not forever!
    for (int i = 0; i < 100; i++)
    {
        try {
            // do your work here;
    
            break; // break the loop if everything is fine
        } catch (TimeOutException) {
            Thread.Sleep(1000);
        }
    }
    

    Note that, TimeOutException should be replaced by the real name of the exception... I don't know if that is the real name.

    Also adjust the sleep time, given in millisecs and the amount of repeats, in the case I presented, 100 repeats of 1000ms yields a maximum wait of 1 minute and 40 seconds, plus the operation time itself.

    0 讨论(0)
  • 2021-01-18 00:40

    Try

    bool failed = false;
    do {
     try
     {
      salesOrdersArray = MagServ.salesOrderList(sessID, filter);
     }
     catch
     {
      failed = true;
     }
    } while(failed);
    

    The behavior you are after might cause an endless loop if this never succeeds though...

    0 讨论(0)
  • 2021-01-18 00:40

    Try something like this:

    var failed = true;
    while (failed)
    {
      try 
      {
        salesOrdersArray = MagServ.salesOrderList(sessID, filter); 
        failed = false;
      }
      catch
      {
      }
    }
    

    Edit: Wow! Great minds think alike! :)

    0 讨论(0)
提交回复
热议问题