Chapter 6. Dealing with Errors

Errors happen all the time; they’re a fact of life:

  • Despite the best efforts of Microsoft Word, an army of highly skilled reviewers and editors, and even your authors, it would be surprising if there wasn’t a typographical error in a book of this length.

  • Although they are relatively few and far between, there are bugs in the .NET Framework—hence the need for occasional service packs.

  • You might type your credit card number for an online transaction and accidentally transpose two digits; or forget to type in the expiration date.

Like it or not, we’re going to have to face up to the fact that there are going to be errors of all kinds to deal with in our software too. In this chapter, we’ll look at various types of errors, the tools that C# and the .NET Framework give us to deal with them, and some strategies for applying those tools.

First, we need to recognize that all errors are not made the same. We’ve classified a few of the more common ones in Table 6-1.

Table 6-1. A far-from-exhaustive list of some common errors

Error

Description/example

Bug

A failure to implement a contract according to its documentation.

Unexpected behavior

A failure to document a contract properly for all expected input.

Unexpected input

A client passes data to a method that is outside some expected range.

Unexpected data type

A client passes data to a method that is not of the expected type.

Unexpected data format

A client passes data to a method in a format that is not recognized.

Unexpected ...

Get Programming C# 4.0, 6th Edition 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.