2012-10-16 2 views
1

데이터베이스에 쓰는 내 메서드에서 아래 코드와 같이 오류를 처리합니다. catch (DbUpdateException ex)에서 예외를 다시 던져서 마지막 catch (Exception ex)에 잡으려고합니다.동일한 메서드에서 예외를 throw하고 catch하십시오.

그렇게 할 수 있습니까? 아래 코드는 그렇게하지 않습니다.

 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"); 
      } 
     } 
+2

. 스택 추적은 계속 재설정됩니다. –

+2

[제발 흐름 제어에 대한 예외를 사용하지 마십시오] (http://stackoverflow.com/questions/729379/why-not-use-exceptions-as-regular-flow-of-control) –

+2

왜 그걸 원하니? 마지막 catch는 모든 예외 세부 사항을 숨기므로 디버깅 및 유지 관리가 더 어려워집니다. 예외를 처리하지 않으려는 경우 거품을 버리십시오. – Oscar

답변

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"); 
     } 
    } 
8

다음은 더 좋을 것이다 :

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

가 거품까지를 제외하고 보자. 당신이해야 할 일이 모두 다시 던지면 잡을 필요가 없습니다.

참고 : throw ex;은 스택 추적을 재설정합니다. throw;을 정상적으로 수행하려고합니다.

+2

+1 그걸로 잡초를 제거 하고이 솔루션을보고. 감사! –

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; 
       } 
      } 
     } 
} 

을 그럼 당신은 당신의 방법은 하나 catch 블록의 try catch 블록으로

try 
{ 
    YourMethod() 
} 
catch (Exception ex) 
{ 
    throw 
     new Exception("Error"); 
} 
+0

같은 방법으로하고 싶습니다. – SilverDeveloper

+1

@ SilverDeveloper - 정확히 무슨 뜻입니까? 이것은 당신이 원하는 것을 정확하게합니다. 그 stil 나쁜 생각이지만. –

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"); 
    } 

} 
+0

감사합니다.하지만 폴이 더 빠릅니다. – SilverDeveloper

+0

@ SilverDeveloper, 문제가 없습니다. –

+0

한가지주의해야 할 점은 @Oded는 *** 매우 좋은 점이며 중첩 된 try-catch 블록의 경로를 사용하기 전에 고려해야 할 사항입니다. –

3

다른 캐치의 예외를 catch하려면 동일한 수준에있을 수 없습니다.

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

당신은 그것을 변경해야합니다 :

현재 코드는이 구조가

try 
{ 

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

을하지만 당신은 매우 신중하게 당신이 정말로/원하는이 필요하면 생각해야한다. 생산적인 오류 처리 패턴처럼 보이지 않습니다.

그리고 예외를 던지기위한 4 가지 방법과 그 결과에 대해서는 this answer을 참조하십시오. 에 의해 설명 된대로 둥지에 시도 - 캐치 블록을 계획 할 때

0

는 "바울은"예외 타입에주의하여야합니다 : 당신은 여기에 유용한 정보의 모든 종류를 잃어 가고있다

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"); 
    } 

}

관련 문제