尝试在try块中捕获 - 异常应该调用相同的函数

时间:2012-08-31 08:09:13

标签: c# .net exception

如果我有这样的东西怎么办:

public static void DoSomething()
{
  try
  { 
    //some code
    try
    {
      //some other code
    }
    catch (Exception e)
    {
      log.Error("At the end something is wrong: " + e);
      FunctionA(); //same function as in the first exception
    }
  }
  catch (Exception e)
  {
    log.Error("At the start something wrong: " + e);
    FunctionA();
  }
}

所以我试着抓住另一个。例外应该是不同的,我想用记录器来区分它们。但是,我想说我想为两个例外调用相同的函数。我必须写FunctionA() 2次。这好吗?或者此类异常还有其他问题吗?建议?

4 个答案:

答案 0 :(得分:2)

您可以使用具有多个catch块的单个try块;并且你也可以使用finally语句来执行某些代码段,无论是否存在异常。

在函数内部使用多个try-catch块通常不是一个好主意。此外,最好在发生的情况下处理异常。

public static void DoSomething()
{
  try
  { 
    //some code
  }

  catch (ExceptionA e)
  {
    // exception is ExceptionA type
    log.Error("At the end something is wrong: " + e);
    FunctionA(); //same function as in the first exception    
  }
  catch (ExceptionB e)
  {
    // exception is ExceptionB type
    log.Error("At the start something wrong: " + e);
    FunctionA(); //same function as in the first exception    
  }
  finally
  {
        //you can do something here whether there is an exception or not
  }
}

答案 1 :(得分:1)

因此,您希望在所有特殊情况下调用方法。然后,如果所有成功执行,我将使用Boolean变量来存储。然后你可以在最后调用FunctionA

public static void DoSomething()
{
    bool success = true;
    try
    {
        //some code
        try
        {
            //some other code
        } catch (Exception ex)
        {
            success = false;
            try
            {
                //some other code
            } catch (ExceptionA eA)
            {
                log.Error("At the end, something went wrong: " + eA);
            } catch (ExceptionB eB)
            {
                log.Error("At the end, something else went wrong: " + eB);
            }
        }
    } catch (Exception ex)
    {
        success = false;
        log.Error("At the start something wrong: " + ex);
    }

    if(!success)
        FunctionA();
}

但是如果您想要在任何情况下调用此方法,这意味着无论是否引发了异常,您都可以使用finally。如果要清理资源,它也很有用。

答案 2 :(得分:0)

您可以将代码放入可重复使用的方法,例如

public void LogIfSomeException(Action action, string message)
{
    try
    {
        action();
    }
    catch (SomeException e)
    {
        log.Error(message + e);
        FunctionA();
    }
}

您可以使用:

public static void DoSomething()       
{
    LogIfSomeException(() => {
       // start code
       LogIfSomeException(() => {
          // end code
       }, "At the end something is wrong");
    }, "At the start something is wrong");

} 

答案 3 :(得分:0)

如果您更改DoSomthing以重新抛出此类例外,

public static void DoSomething()
{
  try
  { 
    //some code
    try
    {
      //some other code
    }
    catch (Exception e)
    {
      log.Error("At the end something is wrong: " + e);
      throw;
    }
  }
  catch (Exception e)
  {
    log.Error("At the start something wrong: " + e);
    throw;
  }
}

这是有道理的,因为记录异常并不是真正处理它,我怀疑FunctionA会做到这一点,但......。当你致电DoSomething时,你可以抓住任何一个例外。

static void CallDoSomething()
{
    try
    {
       DoSomething();
    }
    catch
    {
       FunctionA();
    }
}

或者,如果你真的想在同一个功能中这样做。

public static void DoSomething()
{
    try
    {
        try
        { 

            // some code

            try
            {

                // some other code

            }
            catch (Exception e)
            {
                log.Error("At the end something is wrong: " + e);
                throw;
            }
        }
        catch (Exception e)
        {
            log.Error("At the start something wrong: " + e);
            throw;
        }
    }
    catch (Exception) // I include the type in the hope it will be more derived.
    {
        FunctionA();
    }
}