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 |
---|---|---|
|
|
Use this class as the base class to user-defined exceptions; a more derived exception should be thrown. |
|
|
Thrown when a parameter value for a method is |
|
|
Thrown when a parameter value for a method is out of the range of expected values. |
|
|
Thrown when an incompatible data type is assigned to an element in an array. |
|
|
Thrown when an unknown |
|
|
Thrown when an invalid configuration setting is encountered. |
|
Get C# Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.