.NET प्रोग्रामिंग दिशानिर्देश बताते हैं कि हमें सामान्य अपवाद नहीं लेना चाहिए। मुझे लगता है निम्नलिखित कोड सामान्य अपवाद प्रकार पकड़ की वजह से बहुत अच्छा नहीं है:.NET कैच सामान्य अपवाद
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)।
क्या यह अन्य सभी व्यक्तिगत अपवादों को पकड़ने के लिए स्वीकार्य है? या कोई बेहतर तरीका है?
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 –