Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Usage Warnings
 Do not raise reserved exception typ...
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
Visual Studio Team System
Do not raise reserved exception types

TypeName

DoNotRaiseReservedExceptionTypes

CheckId

CA2201

Category

Microsoft.Usage

Breaking Change

Breaking

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

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

The following exception types are reserved and should be thrown only by the common language runtime:

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.

Do not exclude a warning from this rule.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
FAQ: Why do some sources recommend throwing ApplicationException while Visual Studio Code Analysis (FxCop) does not?      David M. Kean - MSFT   |   Edit   |  
See the following post on the Visual Studio Code Analysis blog:
http://blogs.msdn.com/fxcop/archive/2006/04/05/569569.aspx
Tags What's this?: Add a tag
Flag as ContentBug
FAQ: Why does FxCop warn against catch(Exception)?      David M. Kean - MSFT   |   Edit   |  

See the following post on the Visual Studio Code Analysis blog:
http://blogs.msdn.com/fxcop/archive/2006/06/14/631923.aspx

Tags What's this?: Add a tag
Flag as ContentBug
Do not throw general exception types      David M. Kean - MSFT ... Sheng Jiang   |   Edit   |  

Throwing a general exception type such as Exception or SystemException in a library or Framework forces consumers to catch all exceptions, including unknown exceptions that they do not know how to handle.

Instead, either throw a more derived type that already exists in the Framework, or create your own type that derives from Exception.

The following list examples of when you should throw specific exceptions:

When validating a parameter (including the value parameter in the set accessor of a property) that:

  • Is a null reference ( Nothing in Visual Basic)
    throw System.ArgumentNullException
  • Is outside of the allowable range of values (such as an index for a Collection/List)
    throw System.ArgumentOutOfRangeException
  • Is an invalid enum value
    throw System.ComponentModel.InvalidEnumArgumentException
  • Contains a format that not meet the parameter specifications of a method (such as the format string for ToString(String))
    throw System.FormatException
  • Is otherwise invalid
    throw System.ArgumentException


When an operation is invalid for an object's current state:
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. For more information, see Krzysztof Cwalina's blog How to Design Exception Hierarchies (http://blogs.msdn.com/kcwalina/archive/2007/01/30/ExceptionHierarchies.aspx)

Tags What's this?: Add a tag
Flag as ContentBug
Okay to throw OutOfMemoryException in some circumstances      anforowicz   |   Edit   |  

You probably want to throw OutOfMemoryException when PInvoked native code returns ERROR_OUTOFMEMORY or a similar result. I discussed this with FxCop authors and they say that it is okay to suppress the rule in such circumstances.

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker