Skip to Content
C# Cookbook
book

C# Cookbook

by Stephen Teilhet, Jay Hilyard
January 2004
Beginner to intermediate
864 pages
22h 18m
English
O'Reilly Media, Inc.
Content preview from C# Cookbook

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.

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

C# Cookbook

C# Cookbook

Joe Mayo
C# Cookbook, 2nd Edition

C# Cookbook, 2nd Edition

Jay Hilyard, Stephen Teilhet
ASP.NET Cookbook

ASP.NET Cookbook

Michael A Kittel, Geoffrey T. LeBlond

Publisher Resources

ISBN: 0596003390Supplemental ContentCatalog PageErrata