DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 5.3 Choosing when to Throw a Particular Exception

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 IndexOutOfRangeException when an array index is greater than the length of the array), 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 5-1 to determine which exception to employ when throwing or catching exceptions.

Discussion

Table 5-1. The built-in exception types

Exception name

Derives from

Description

System.ApplicationException

Exception

Use this class as the base class to user-defined exceptions; a more derived exception should be thrown.

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 an invalid format parameter is passed to a method.

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 yet 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.

    [ Team LiB ] Previous Section Next Section