예외는 명시적으로 다시 발생되어서는 안 됩니다.

반응형

예외를 다시 발생시킬 때는 간단히 throw를 호출하여 수행해야 합니다. ex;를 던지지 마세요. 스택 추적이 두 번째 구문으로 재설정되어 디버깅이 훨씬 더 어려워지기 때문입니다.

예:

try
{}
catch(ExceptionType1 exc)
{
  Console.WriteLine(exc);
  throw exc; // Noncompliant; stacktrace is reset
}
catch(ExceptionType2 exc)
{
  Console.WriteLine(exc);
  throw;     // Compliant
}
catch (ExceptionType3 exc)
{
  throw new Exception("My custom message", exc);  // Compliant; stack trace preserved
}
반응형