title: .NET Catch General Exceptions
body: <p>.NET Programming guidelines state that we should not catch general exceptions. I assume the following code is not very good because of the general exception type catch:</p>
  
  <pre><code>    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;
      }
  </code></pre>
  
  <p>In the following code I catch particular exceptions but not all of them and then I re-throw the exception in case is different from the non-generic exceptions. However the function "CreateInstance" can throw many exceptions (ArgumentNullException, ArgumentException, NotSupportedException, TargetInvocationException, MethodAccessException, MemberAccessException, InvalidComObjectException, MissingMethodException, COMException, TypeLoadException).</p>
  
  <p>Is it acceptable to catch all other individual exceptions? Or is there a better way?</p>
  
  <pre><code>    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;
      }
  </code></pre>