C Sharp

Error Handling with Exceptions

One of the main goals of the .NET Common Language Runtime (CLR) is for run-time errors to either be avoided (through features such as automatic memory and resource management when using managed code) or at least caught at compile time (by a strongly typed system).

However, certain errors can be caught only at run time, and therefore a consistent means of dealing with errors must be used across all the languages that comply with the Common Language Specification (CLS). To that extent, this chapter focuses on the error-handling system implemented by the CLR—exception handling.-

In this chapter, you'll first learn the general mechanics and basic syntax of exception handling. Once you have this baseline knowledge, you'll see how exception handling compares with the more prevalent methods of error handling today and you'll discover the advantages that exception handling has over these other techniques. We'll then dive into some of the more specific .NET exception-handling issues, such as using the Exception class and deriving your own exception classes. Finally, the last part of this chapter will deal with the issue of properly designing your system to use exception handling.