データベースに書き込む私のメソッドでは、次のコードに示すようにエラーを処理します。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");
}
}