Handling and Throwing Exceptions

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. Finally, the runtime's exception handling is faster than Windows-based C++ error handling.

Because execution threads routinely traverse managed and unmanaged blocks of code, the runtime can throw or catch exceptions in either managed or unmanaged code. Unmanaged code can include both C++-style SEH Exceptions and COM-based HRESULTS.

In This Section

Exceptions Overview
Provides an overview of common language runtime exceptions.
Exception Class
Describes the elements of an exception object.
Exception Hierarchy
Describes the exceptions that most exceptions derive from.
Exception Handling Fundamentals
Explains how to handle exceptions using catch, throw, and finally statements.
Best Practices for Handling Exceptions
Describes suggested methods for handling exceptions.
Handling COM Interop Exceptions
Describes how to handle exceptions thrown and caught in unmanaged code.

Related Sections

Exception Class
Reference information for the class that all exceptions inherit from.
ApplicationException Class
Reference information for the class that all application-generated exceptions should derive from.
SystemException Class
Reference information for the class that all system-generated exceptions derive from.
Advanced COM Interop
Describes how exceptions work between managed and unmanaged code.
HRESULTs and Exceptions
Describes the mapping of exceptions between managed and unmanaged code.