高效的异常处理技术 [英] Efficient exception handling techniques

查看:157
本文介绍了高效的异常处理技术的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写在C#中,需要我在一些方面,用户通过在即时创建一个阵列对象的应用程序的阵列.CreateInstance()方法(按最新统计)6种不同的例外情况,我会要处理抛出。对于每一个例外,我想通过一个简单的的MessageBox.show(),并针对特殊情况的消息通知用户。我不想做的就是抓住一般异常类型,因为它是这样做的最佳实践。我想尝试追赶的ArgumentException 或更具体的东西,但唯一的公共超所有的例外是异常

底线:我试图找出最好的方式来处理有这么多不同的异常,而这将是一个有效的,更重要的,维护的解决方案

 尝试
{
    数据= Array.CreateInstance(TypeHelper.StringToType(cbDataType.SelectedItem.ToString()),外形尺寸);
}
赶上(OutOfMemoryException异常){}
赶上(NullReferenceException异常){}
赶上(NotSupportedException异常){}
赶上(ArgumentNullException){}
赶上(ArgumentOutOfRangeException){}
赶上(ArgumentException的){}
 

解决方案

这名单有我会考虑捕捉只有4例外:

  • NotSupportedException异常
  • ArgumentNullException
  • ArgumentOutOfRangeException
  • 的ArgumentException

另外两个你应该永远赶不上,并为后来的CLR的,你不能捕获一个OOM情况(考虑的 MemoryFailPoint )。

更深入地研究 Array.CreateInstance ,我们看到为什么每个这四个会被抛出:

  • NotImplementedException :你给它不可能是一个数组,或者是一个开放的泛型类型。既然你是从一个固定的名单拉这些数据类型,你应该知道的的先验的,这些都是有效的类型。我认为对处理该异常。
  • ArgumentNullException :你应该确保所有的传递参数​​是不为空,因此这将不会发生,你不应该处理这个异常
  • ArgumentOutOfRangeException :长度的一个小于0,它可以测试的的先验的,所以你不应该处理这个异常
  • 的ArgumentException :如果抛出的类型是无效的(你已经确定它是有效的),或者如果没有足够的长度,你可以测试的先验

所以,我的建议code将是:

  // $ C $在此之前,C点确保了cbDataType只有正确的类型
//和尺寸具有至少1的尺寸,并且比所有大于或等于1
数据= Array.CreateInstance(
    TypeHelper.StringToType(cbDataType.SelectedItem.ToString()),
    尺寸);
 

总之,我不会处理任何异常,因为你应该能够prevent所有这些的发生,你不应该关心的情况下,你不可能处理异常。

I am writing an application in C# that requires me to create an Array object on the fly from some dimensions the user passes in. The Array.CreateInstance() method can throw (by last count) 6 different exceptions that I would want to handle. For each exception I would want to inform the user by a simple MessageBox.Show() and a message tailored to the exceptional circumstance. What I do not want to do is catch the general Exception type, because it is a best practice to not do so. I would try catching ArgumentException or something more specific, but the only common superclass to all of the exceptions is Exception.

Bottom Line: I am trying to figure out the best way to handle having so many different exceptions, and what would be an efficient and, more importantly, maintainable solution.

try
{
    data = Array.CreateInstance(TypeHelper.StringToType(cbDataType.SelectedItem.ToString()), dimensions);
}
catch (OutOfMemoryException) { }
catch (NullReferenceException) { }
catch (NotSupportedException) { }
catch (ArgumentNullException) { }
catch (ArgumentOutOfRangeException) { }
catch (ArgumentException) { }

解决方案

Of that list there are only 4 exceptions I would consider catching:

  • NotSupportedException
  • ArgumentNullException
  • ArgumentOutOfRangeException
  • ArgumentException

The other two you should never catch, and as of the later CLR's you can't catch an OOM situation (consider MemoryFailPoint if you need to find out).

Delving deeper into Array.CreateInstance, we see why each of those four would be thrown:

  • NotImplementedException: the type you gave it can't be an array, or is an open generic. Since you are pulling these data types from a fixed list, you should know a priori that these are valid types. I would argue against handling this exception.
  • ArgumentNullException: you should be certain all of the arguments you pass are not null, thus this will never happen and you should not handle this exception.
  • ArgumentOutOfRangeException: one of the lengths is less than 0, which you can test a priori, thus you should not handle this exception.
  • ArgumentException: Thrown if the type is invalid (you've already made sure it is valid) or if there aren't enough lengths, which you can test a priori.

So, my suggested code would be:

// code prior to this point ensures cbDataType only has correct types
// and dimensions has at least 1 dimension and is all greater than or equal to 1
data = Array.CreateInstance(
    TypeHelper.StringToType(cbDataType.SelectedItem.ToString()),
    dimensions);

In summary, I would not handle any exceptions since you should be able to prevent all of them from occurring and you shouldn't care about the instances where you can't possibly handle the exception.

这篇关于高效的异常处理技术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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