Freigeben über


Choosing the Right Type of Exception to Throw 

The following design guidelines help ensure that you use the existing exceptions, where appropriate, and create new exceptions where they add value to your library.

Consider throwing existing exceptions residing in the System namespaces instead of creating custom exception types.

For detailed guidelines on the most frequently used exception types provided by the .NET Framework, see Catching and Throwing Standard Exception Types.

Do create and throw custom exceptions if you have an error condition that can be programmatically handled in a different way than any other existing exceptions. Otherwise, throw one of the existing exceptions.

For detailed guidelines on creating custom exceptions, see Designing Custom Exceptions.

Do not create and throw new exceptions just to have your team's exception.

Do throw the most specific (the most derived) exception that is appropriate. For example, if a method receives a null (Nothing in Visual Basic) argument, it should throw System.ArgumentNullException instead of its base type System.ArgumentException.

Portions Copyright 2005 Microsoft Corporation. All rights reserved.

Portions Copyright Addison-Wesley Corporation. All rights reserved.

For more information on design guidelines, see the "Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries" book by Krzysztof Cwalina and Brad Abrams, published by Addison-Wesley, 2005.

See Also

Concepts

Exception Throwing

Other Resources

Design Guidelines for Developing Class Libraries
Design Guidelines for Exceptions