以相同的方法抛出并捕获异常

时间:2012-10-16 11:14:09

标签: c# .net asp.net-mvc entity-framework

在我写入数据库的方法中,我处理错误,如下面的代码所示。在catch (DbUpdateException ex)中,我想重新抛出异常并在最后catch (Exception ex)中捕获它。

这可能吗?怎么做?下面的代码没有这样做。

        using (Entities context = new Entities())
        {
            try
            {
                context.Office.Add(office);
                retVal = context.SaveChanges();
            }
            catch (DbUpdateException ex)
            {
                SqlException innerException = ex.GetBaseException() as SqlException;
                if (innerException != null && innerException.Number == (int)SQLErrorCode.DUPLICATE_UNIQUE_CONSTRAINT)
                {
                    throw
                        new Exception("Error ocurred");
                }
                //This is momenty where exception is thrown.
                else
                {
                    throw ex;
                }
            }
            catch (Exception ex)
            {
                throw
                    new Exception("Error");
            }
        }

6 个答案:

答案 0 :(得分:8)

以下情况会更好:

context.Office.Add(office);
retVal = context.SaveChanges();

让泡沫除外。如果你要做的就是重新投掷,就不需要抓住东西了。

注意:throw ex;将重置堆栈跟踪 - 您希望正常执行throw;

答案 1 :(得分:3)

如果你想从其他捕获中捕获异​​常,那么它们就不能处于同一级别。

您当前的代码具有以下结构:

try
{
}
catch (...)
{
}
catch (...)
{
}

您需要将其更改为:

try
{

    try
    {
    }
    catch (...)
    { 
       // throw X
    }                
}
catch (...)
{
   // catch X here
}

但如果你真的想要/需要这个,你应该仔细考虑。它看起来不像是一种高效的错误处理模式。

请参阅this answer了解(重新)抛出异常的4种不同方法及其后果。

答案 2 :(得分:1)

try-catch仅处理一个捕获块,并按顺序对它们进行评估。因此,如果您真的需要此功能,则需要在try-catch中放置try-catch,如下所示:

using (Entities context = new Entities()) 
{ 
    try
    {
        try 
        { 
            context.Office.Add(office); 
            retVal = context.SaveChanges(); 
        } 
        catch (DbUpdateException ex) 
        { 
            SqlException innerException = ex.GetBaseException() as SqlException; 
            if (innerException != null && innerException.Number == (int)SQLErrorCode.DUPLICATE_UNIQUE_CONSTRAINT) 
            { 
                throw 
                    new Exception("Error ocurred"); 
            } 
            //This is momenty where exception is thrown. 
            else 
            { 
                throw ex; 
            } 
        } 
    }
    catch (Exception ex) 
    { 
        throw 
            new Exception("Error"); 
    } 

} 

答案 3 :(得分:1)

您是否尝试过嵌套try...catch块?

using (Entities context = new Entities())
    {
        try
        {
            try
            {
                context.Office.Add(office);
                retVal = context.SaveChanges();
            }
            catch (DbUpdateException ex)
            {
                SqlException innerException = ex.GetBaseException() as SqlException;
                if (innerException != null && innerException.Number == (int)SQLErrorCode.DUPLICATE_UNIQUE_CONSTRAINT)
                {
                    throw new Exception("Error ocurred");
                }
                //This is momenty where exception is thrown.
                else
                {
                    throw ex;
                }
            }
        }
        catch (Exception ex)
        {
            throw new Exception("Error");
        }
    }

答案 4 :(得分:0)

试试这个:

void YourMethod()
{
using (Entities context = new Entities())
        {
            try
            {
                context.Office.Add(office);
                retVal = context.SaveChanges();
            }
            catch (DbUpdateException ex)
            {
                SqlException innerException = ex.GetBaseException() as SqlException;
                if (innerException != null && innerException.Number == (int)SQLErrorCode.DUPLICATE_UNIQUE_CONSTRAINT)
                {
                    throw
                        new Exception("Error ocurred");
                }
                //This is momenty where exception is thrown.
                else
                {
                    throw ex;
                }
            }
        }
}

然后当你调用你的方法时,用try catch块

将它括起来
try
{
     YourMethod()
}
catch (Exception ex)
{
     throw
         new Exception("Error");
}

答案 5 :(得分:0)

当您计划按照“paul”所述嵌套try-catch-block时,请注意异常类型:

using (Entities context = new Entities())      
{          
  try
  {
      try
      {
          context.Office.Add(office);
          retVal = context.SaveChanges();
      }
      catch (DbUpdateException ex)
      {
          SqlException innerException = ex.GetBaseException() as SqlException;
          if (innerException != null && innerException.Number == (int)SQLErrorCode.DUPLICATE_UNIQUE_CONSTRAINT)
          {
              // this exception will be catched too in outer try-catch block <--------
              throw new Exception("Error ocurred");
          }
          //This is momenty where exception is thrown.
          else
          {
              throw ex;
          }
      }
  }
  // Catch (DbUpdateException  ex) if you plan to have the rethrown exception to be catched <------------
  catch (Exception ex)
  {
      throw new Exception("Error");
  }

}