2

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

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

4 回答 4

2

您可以将单个 try 块与多个 catch 块一起使用;并且您也可以使用 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
  }
}
于 2012-08-31T08:12:00.460 回答
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. 如果您想清理资源,它也很有用。

于 2012-08-31T08:15:45.073 回答
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();
    }
}
于 2012-08-31T08:26:46.210 回答
0

You could put your code into a reusble method e.g.

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

Which you could use like:

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

} 
于 2012-08-31T08:20:39.010 回答