在C#中抛出保留的异常会出现什么问题?

如果在代码中抛出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.

相关文章

目录简介使用JS互操作使用ClipLazor库创建项目使用方法简单测...
目录简介快速入门安装 NuGet 包实体类User数据库类DbFactory...
本文实现一个简单的配置类,原理比较简单,适用于一些小型项...
C#中Description特性主要用于枚举和属性,方法比较简单,记录...
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机...
目录简介获取 HTML 文档解析 HTML 文档测试补充:使用 CSS 选...