Handling and Throwing Exceptions
Microsoft Silverlight will reach end of support after October 2021. Learn more.
Programs must be able to uniformly handle errors that occur during execution. The common language runtime greatly assists the design of fault-tolerant software by providing a model for notifying programs of errors in a uniform way. All .NET Framework operations indicate failure by throwing exceptions.
Traditionally, a language's error handling model relied on either the language's unique way of detecting errors and locating handlers for them, or on the error handling mechanism provided by the operating system. The runtime implements exception handling with the following features:
Handles exceptions without regard for the language that generates the exception or the language that handles the exception.
Does not require any particular language syntax for handling exceptions, but allows each language to define its own syntax.
Allows exceptions to be thrown across process and even machine boundaries.
Exceptions offer several advantages over other methods of error notification, such as return codes. Failures do not go unnoticed. Invalid values do not continue to propagate through the system. You do not have to check return codes. Exception-handling code can be easily added to increase program reliability.
In This Section
Exception Class and Properties
Describes the elements of an exception object.
Describes the exceptions that most exceptions derive from.
Explains how to handle exceptions using catch, throw, and finally statements.
Best Practices for Handling Exceptions
Describes suggested methods for handling exceptions.