Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What can go wrong when throwing a reserved exception in C#? [closed]

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?

like image 223
Paul Jansen Avatar asked Feb 12 '13 13:02

Paul Jansen


1 Answers

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.

like image 90
Spontifixus Avatar answered Oct 01 '22 16:10

Spontifixus