Previous Page
Next Page

Coping with Errors

It's a fact of life that bad things sometimes happen. Tires get punctured, batteries run down, screwdrivers are never where you left them, and users of your applications behave in an unpredictable manner. Errors can occur at almost any stage when a program runs, so how do you detect them and attempt to recover? Over the years, a number of mechanisms have evolved. A typical approach adopted by older systems such as Unix involved arranging for the operating system to set a special global variable whenever a method failed. Then, after each call to a method, you checked the global variable to see whether the method failed. This solution has a number of shortcomings:

For these reasons, C# and most other modern object-oriented languages don't handle errors in this way. It's just too painful. They use exceptions instead. If you want to write robust C# programs, you need to know about exceptions.


Previous Page
Next Page