.NET捕捉一般例外 [英] .NET Catch General Exceptions

查看:146
本文介绍了.NET捕捉一般例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.NET编程指南指出,我们不应该赶上一般例外。我想下面的code是因为一般的异常类型的制止不太好:

 私有对象的CreateObject(字符串类名)
    {
        obj对象= NULL;
        如果(!string.IsNullOrEmpty(类名))
        {
           尝试
           {
              System.Type的oType = System.Type.GetTypeFromProgID(customClass);
              OBJ = System.Activator.CreateInstance(oType);
           }
           赶上(例外前)
           {
               Log.Error(无法创建实例的COM对象与类名+类名+\ N+ ex.Message);
           }
        }
        返回OBJ;
    }
 

在以下code我赶上特殊异常,但不是所有的人,然后我重新抛出异常的情况下,是从非一般异常不同。然而,功能的CreateInstance可以引发许多异常(ArgumentNullException,ArgumentException的,引发NotSupportedException,TargetInvocationException,MethodAccessException,MemberAccessException,InvalidComObjectException,的MissingMethodException,收到COMException,TypeLoadException)。

是可以接受捕捉所有其他的个别例外?还是有更好的办法吗?

 私有对象的CreateObject(字符串类名)
    {
        obj对象= NULL;
        如果(!string.IsNullOrEmpty(类名))
        {
           尝试
           {
              System.Type的oType = System.Type.GetTypeFromProgID(customClass);
              OBJ = System.Activator.CreateInstance(oType);
           }
           赶上(NotSupportedException异常前)
           {
              Log.Error(....+ ex.Message);
           }
           赶上(TargetInvocationException前)
           {
              Log.Error(....+ ex.Message);
           }
           赶上(收到COMException前)
           {
              Log.Error(....+ ex.Message);
           }
           赶上(TypeLoadException前)
           {
              Log.Error(....+ ex.Message);
           }
           赶上(InvalidComObjectException前)
           {
              Log.Error(....+ ex.Message);
           }
           赶上(例外前)
           {
               Log.Error(无法创建实例的COM对象与类名+类名+\ N+ ex.Message);
               扔;
           }
        }
        返回OBJ;
    }
 

解决方案

这是完全可以接受的捕捉一般例外的框架.NET 2+。

- 修改

唯一的原因,你不会,是你可以做的事情有不同的异常不同。如果你打算如何处理它们都是一样的,正好赶上一般(你是后或特定的一个,而让别的去了)。

.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:

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

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).

Is it acceptable to catch all other individual exceptions? Or is there a better way?

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

解决方案

It's quite acceptable to catch general exceptions in .NET 2+ of the framework.

-- Edit

The only reason you wouldn't, is if you can do something different with a different exception. If you plan on handling them all the same, just catch the general (or specific one you are after, and let anything else go up).

这篇关于.NET捕捉一般例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆