Chapter 16Errors and Exceptions

WHAT’S IN THIS CHAPTER?

  • Looking at the exception classes
  • Using try. . .catch. . .finally to capture exceptions
  • Creating user-defined exceptions
  • Retrieving caller information

WROX.COM CODE DOWNLOADS FOR THIS CHAPTER

The wrox.com code downloads for this chapter are found at www.wrox.com/go/procsharp on the Download Code tab. The code for this chapter is divided into the following major examples:

  • Simple Exceptions
  • Solicit Cold Call
  • Caller Information

INTRODUCTION

Errors happen, and they are not always caused by the person who coded the application. Sometimes your application will generate an error because of an action that was initiated by the end user of the application, or it might be simply due to the environmental context in which your code is running. In any case, you should anticipate errors occurring in your applications and code accordingly.

The .NET Framework has enhanced the ways in which you deal with errors. C#’s mechanism for handling error conditions enables you to provide custom handling for each type of error condition, as well as to separate the code that identifies errors from the code that handles them.

No matter how good your coding is, your programs should be capable of handling any possible errors that may occur. For example, in the middle of some complex processing of your code, you may discover that it doesn’t have permission to read a file; or, while it is sending network requests, the network may go down. In such exceptional ...

Get Professional C# 5.0 and .NET 4.5.1 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.