.NET 프로그래밍 가이드 라인에는 일반적인 예외는 발견하지 않아야한다고 명시되어 있습니다. 경우에 다른 내가 특정 예외는 아니지만 그들 모두와 나는 예외를 다시 던질 잡을 다음 코드에서.NET Catch 일반 예외
private object CreateObject(string classname)
{
object obj = null;
if (!string.IsNullOrEmpty(classname))
{
try
{
System.Type oType = System.Type.GetTypeFromProgID(customClass);
obj = System.Activator.CreateInstance(oType);
}
catch (Exception ex)
{
Log.Error("Unable to create instance for COM Object with class name " + classname + "\n" + ex.Message);
}
}
return obj;
}
: 다음 코드 때문에 일반 예외 유형 캐치의 매우 좋지 않아 가정 비 제네릭 예외 그러나 "CreateInstance"함수는 많은 예외 (ArgumentNullException, ArgumentException, NotSupportedException, TargetInvocationException, MethodAccessException, MemberAccessException, InvalidComObjectException, MissingMethodException, COMException, TypeLoadException)를 throw 할 수 있습니다.
다른 모든 개별 예외를 포착해도됩니까? 아니면 더 좋은 방법이 있습니까?
private object CreateObject(string classname)
{
object obj = null;
if (!string.IsNullOrEmpty(classname))
{
try
{
System.Type oType = System.Type.GetTypeFromProgID(customClass);
obj = System.Activator.CreateInstance(oType);
}
catch (NotSupportedException ex)
{
Log.Error("...." + ex.Message);
}
catch (TargetInvocationException ex)
{
Log.Error("...." + ex.Message);
}
catch (COMException ex)
{
Log.Error("...." + ex.Message);
}
catch (TypeLoadException ex)
{
Log.Error("...." + ex.Message);
}
catch (InvalidComObjectException ex)
{
Log.Error("...." + ex.Message);
}
catch (Exception ex)
{
Log.Error("Unable to create instance for COM Object with class name " + classname + "\n" + ex.Message);
throw;
}
}
return obj;
}
중복 : http://stackoverflow.com/questions/204814/is-there-any-valid-reason-to-ever-ignore-a-caught-exception –