Exception handling Exception handling in C# is pretty much like exception handling in C++, in which you use a try. . . catch block. If you don't catch an exception in C#, it will display it as a nasty message box to the user and then terminate the application. C# also adds the keyword finally. Exception handling works in this way: The try block executes the code within it, and if an exception occurs anywhere within that block, the code jumps directly to the catch block. If the exception in the catch block matches the exception thrown, the catch block is executed and the program continues. The finally block is similar to a catch block, except the code in the finally block is executed regardless of whether an exception is thrown. There are some built-in exception types in C#.Table 1-11. Exception types in C#
Listing 1-38 is an example of a NullReference exception.Listing 1-38. a null reference exception
}The Exception object catches all exceptions. The following code catches the overflow exception with the following output to the console:Caught the Exception - An exception of type System.IndexOutOfRangeException was thrown.The value = 5Documenting your source codeAnother good feature of C# language is its programmatic support of source-code documentation. The source- code documentation is a good programming practice of software development lifecycle for long projects. C# language supports programmatic documentation with the help of XML files. You store documentation in an XML file, tell the compiler to look for the file and the compiler puts the documentation for you in the source code. The comments in the source code start with three slashes (/ / /) instead of two (/ /).You can even set documentation using the VS.NETIDE. You go to project's properties page by right- clicking on the project from Solution Explorer, going to build Properties, and setting the XML file as XML Documentation File option.MSDN documentation has a nice tutorial on XML documentation file under visual studio .NET/visual basic and visual C#/C# Programmer's Reference/C# Language Features/XML Documentation.SummaryThis article offered an overview of the new Microsoft language, C#. C# takes the best features of many present -day programming languages. You became familiar with some of the language's syntax. You learned how to write and compile your first command-line program with the "Hello, C# World!" example. You also became familiar with classes and their members, their scopes, and how to use them. You learned about some unique features, such as events the indexers, which were not available in languages such as C++. You also looked at some useful objects in C#, such as arrays and exceptions.