What can go wrong when throwing a reserved exception in C#?
FxCop issues a violation of rule CA2201 if you throw System.IndexOutOfRangeException
in your code (see reference). The rationale for this is that System.IndexOutOfRangeException
is "reserved and should be thrown only by the common language runtime" according to the documentation.
But what can actually go wrong if you throw System.IndexOutOfRangeException
?
Nothing.
From a technical point of view it is perfectly fine to throw this exception. Nothing will break if you do.
But keep in mind that you should throw the System.IndexOutOfRangeException
only if you have encountered a System.IndexOutOfRangeException
the first place, because otherwise this exception would not be appropriate as it is very clearly defined what this exception type is to be used for. 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.
链接地址: http://www.djcxy.com/p/69328.html下一篇: 在C#中抛出保留的异常时会出现什么问题?