Exception Handling in C#

One area where both C# and the .NET Framework significantly improve on C++ and COM/Win32 is in the area of exception handling. C# supports the same structured try/catch statements for exception handling that C++ has, but it adds a few key improvements. To use C# exception handling, simply enclose the code that may throw an exception inside a try block and then add catch blocks for each type of exception that you would like to handle. An example is as follows:

 try { Console.WriteLine("Enter the numerator"); string strNumerator=Console.ReadLine(); int intNumerator=int.Parse(strNumerator); Console.WriteLine("Enter the denominator"); string strDenominator=Console.ReadLine(); int intDenominator=int.Parse(strDenominator); ...

Get .NET and COM Interoperability Handbook, The 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.