1

在我写入数据库的方法中,我处理错误,如下面的代码所示。在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");
            }
        }
4

6 回答 6

8

以下会更好:

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

让除了冒泡。如果您要做的只是重新投掷,则无需抓住东西。

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

于 2012-10-16T11:17:18.780 回答
3

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

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

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

您需要将其更改为:

try
{

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

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

并查看此答案以了解(重新)抛出异常及其后果的 4 种不同方法。

于 2012-10-16T11:21:44.470 回答
2

你试过嵌套你的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");
        }
    }
于 2012-10-16T11:19:36.093 回答
1

try-catch处理一个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"); 
    } 

} 
于 2012-10-16T11:19:23.600 回答
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");
}
于 2012-10-16T11:18:23.533 回答
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");
  }

}

于 2012-10-16T11:30:57.887 回答