Recipe7.3.Identifying Exceptions and Their Usage


Recipe 7.3. Identifying Exceptions and Their Usage

Problem

There are many exceptions to choose from in the FCL. You need an easily accessible list of these exceptions that indicates when and where to use them. By throwing exceptions in a consistent manner (e.g., throwing an ArgumentNullException when an an argument is null), you and others on your team will be able to debug problems more easily.

Solution

Use the list of exceptions and their definitions in Table 7-1 to determine which exception to employ when throwing or catching exceptions.

Discussion

Table 7-1. The built-in exception types

Exception name

Derives from

Description

System.ApplicationException

Exception

Thrown when a nonfatal application error occurs.

System.ArgumentNullException

ArgumentException

Thrown when a parameter value for a method is null and null is not allowed.

System.ArgumentOutOfRangeException

ArgumentException

Thrown when a parameter value for a method is out of the range of expected values.

System.ArrayTypeMismatchException

SystemException

Thrown when an incompatible data type is assigned to an element in an array.

System.Runtime.InteropServices.COMException

ExternalException

Thrown when an unknown HRESULT is returned from a COM object.

System.Configuration.ConfigurationException

SystemException

Thrown when an invalid configuration setting is encountered.

System.Reflection.CustomAttributeFormatException

FormatException

Thrown when a custom attribute format is incorrect.

System.IO.DirectoryNotFoundException

IOException

Thrown when a file or directory cannot be found.

System.Exception

Object

Base class of all exceptions; you should always throw a more derived exception.

System.FormatException

SystemException

Thrown when a string is not in the expected format.

System.IndexOutOfRangeException

SystemException

Thrown when you attempt to access an array element with an index value outside the valid range for that array.

System.Configuration.Install.InstallException

SystemException

Thrown during software installation when an error is encountered during uninstall, committing of data, or rolling back of data.

System.ComponentModel.InvalidEnumArgumentException

ArgumentException

Thrown when an invalid enumeration value is passed to a method.

System.InvalidOperationException

SystemException

Thrown when a method is called while the object it resides in is in a state that makes it illegal to call this method.

System.IO.IOException

SystemException

Thrown when a general I/O exception occurs; you should throw a more derived exception.

System.MemberAccessException

SystemException

Thrown when a general error occurs while using a class member; you should throw a more derived exception.

System.MethodAccessException

MemberAccessException

Thrown when a general error occurs while using a method member.

System.NotFiniteNumberException

ArithmeticException

Thrown when a double or single data type is expected to have a finite number and instead it contains NaN, +infinity, or -infinity.

System.NotImplementedException

SystemException

Thrown when a member is accessed that is not yet implemented.

System.NotSupportedException

SystemException

Thrown when a member is accessed that is not supported.

System.NullReferenceException

SystemException

Thrown when a reference set to null is used.

System.ObjectDisposedException

InvalidOperation-Exception

Thrown when a disposed object is accessed.

System.ServiceProcess.TimeoutException

SystemException

Thrown when a service times out.

System.ComponentModel.WarningException

SystemException

Thrown when a warning message needs to be displayed. This exception does not imply a serious failure of the application or system.

System.Net.WebException

InvalidOperation-Exception

Thrown when a pluggable protocol causes an error.

System.Xml.XmlException

SystemException

Thrown due to a general error in the XML.


See Also

See the "Exception Class" topic in the MSDN documentation; also see the classes that derive from the Exception class.



C# Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net