Pattern to avoid nested try catch blocks?

后端 未结 16 558
無奈伤痛
無奈伤痛 2020-12-12 12:53

Consider a situation where I have three (or more) ways of performing a calculation, each of which can fail with an exception. In order to attempt each calculation until we f

16条回答
  •  半阙折子戏
    2020-12-12 13:32

    What about tracking the actions your doing...

    double val;
    string track = string.Empty;
    
    try 
    { 
      track = "Calc1";
      val = calc1(); 
    
      track = "Calc2";
      val = calc2(); 
    
      track = "Calc3";
      val = calc3(); 
    }
    catch (Exception e3)
    {
       throw new NoCalcsWorkedException( track );
    }
    

提交回复
热议问题