Appendix A. Exception Handling in .NET
The handling of exceptions and errors is critical to the development of reliable and stable applications. You may have noticed that no exception handling was included in the sample applications in this book. There are several reasons for this. First, omitting exception handling code simply makes the source a lot easier to read. Second, you might encounter new objects that we haven't discussed yet. After you read this appendix, you should implement error handling (also known as exception management) in your applications. Efficient exception management allows developers to write reliable and robust code that helps anticipate exceptions and, in doing so, provides an opportunity to present more informative and user-friendly error messages.
If you come from a C++ background, you are probably familiar with techniques such as C++ exception handling, structured exception handling, and MFC exceptions. If you come from a Visual Basic background, you are probably familiar with the On Error statement. Before .NET, every language implemented its own error handling. With .NET, all languages that create managed code share the same error handling mechanism. All .NET-supported languages (C#, VC++.NET, VB.NET, VJ#) enjoy the same rich exception handling.
C++ developers will probably be familiar with the try...catch block, which provides structured exception handling. Suspect code is placed within a try block, and when an exception occurs, the control is directed to the catch block. We will discuss the try...catch block in more detail in the following sections.
Note
C++ and C# are case-sensitive languages; VB.NET is not, in the sense that no matter what is typed in, the editor automatically corrects the capitalization. In C# and C++, the statement is try...catch; in VB.NET, it is Try...Catch.