How can I improve this exception retry scenario?

前端 未结 10 1560
暗喜
暗喜 2020-12-13 19:39

I have a web service method I am calling which is 3rd party and outside of my domain. For some reason every now and again the web service fails with a gateway timeout. Its i

10条回答
  •  鱼传尺愫
    2020-12-13 20:24

    Here's another way you might try:

    // Easier to change if you decide that 5 retries isn't right for you
    Exception exceptionKeeper = null;
    for (int i = 0; i < MAX_RETRIES; ++i)
    {
        try
        {
           MDO = OperationsWebService.MessageDownload(MI);
           break;  // correct point from Joe - thanks.
        }
        catch (Exception ex)
        {
            exceptionKeeper = ex;
            // 5 retries, ok now log and deal with the error.
        }  
    }
    

    I think it documents the intent better. It's less code as well; easier to maintain.

提交回复
热议问题