如果在代码中抛出System.IndexOutOfRangeException,则FxCop会违反规则CA2201(
see reference).根据文档,System_IndexOutOfRangeException是“保留的,应该只由公共语言运行库抛出”的基本原理.
但是,如果抛出System.IndexOutOfRangeException,实际上会出现什么问题?
解决方法
没有.
从技术角度来看,抛出这个异常是完全可以的.如果你这样做,什么都不会破坏.
但请记住,只有在第一个地方遇到System.IndexOutOfRangeException时才应抛出System.IndexOutOfRangeException,因为否则此异常将不合适,因为它非常清楚地定义了此异常类型的用途. MSDN states for the System.IndexOutOfRangeException:
The exception that is thrown when an attempt is made to access an element of an array with an index that is outside the bounds of the array. This class cannot be inherited.