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は1つの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;
                }
            }
        }
}

次に、メソッドを呼び出すときに、trycatchブロックで囲みます

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 に答える