Domanda

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?

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top