CA2201: Do not raise reserved exception types

Note

This article applies to Visual Studio 2015. If you're looking for Visual Studio 2017 documentation, use the version selector at the top left. We recommend upgrading to Visual Studio 2017. Download it here.

TypeName DoNotRaiseReservedExceptionTypes
CheckId CA2201
Category Microsoft.Usage
Breaking Change Breaking

Cause

A method raises an exception type that is too general or that is reserved by the runtime.

Rule Description

The following exception types are too general to provide sufficient information to the user:

Parameter Description Exception
null reference System.ArgumentNullException
Outside the allowed range of values (such as an index for a collection or list) System.ArgumentOutOfRangeException
Invalid enum value System.ComponentModel.InvalidEnumArgumentException
Contains a format that does not meet the parameter specifications of a method (such as the format string for ToString(String)) System.FormatException
Otherwise invalid System.ArgumentException

When an operation is invalid for the current state of an object throw System.InvalidOperationException

When an operation is performed on an object that has been disposed throw System.ObjectDisposedException

When an operation is not supported (such as in an overridden Stream.Write in a Stream opened for reading) throw System.NotSupportedException

When a conversion would result in an overflow (such as in a explicit cast operator overload) throw System.OverflowException

For all other situations, consider creating your own type that derives from Exception and throw that.

How to Fix Violations

To fix a violation of this rule, change the type of the thrown exception to a specific type that is not one of the reserved types.

When to Suppress Warnings

Do not suppress a warning from this rule.

CA1031: Do not catch general exception types