Exception Handling
Microsoft Silverlight will reach end of support after October 2021. Learn more.
The common language runtime supports an exception handling model based on the concepts of exception objects and protected blocks of code. The runtime creates an object to represent an exception when it occurs. You can also create your own exception classes by deriving classes from the appropriate base exception.
You can explicitly throw an exception using the throw statement. You can also throw a caught exception again using the throw statement. It is good coding practice to add information to an exception that is re-thrown to provide more information when debugging.
All languages that use the runtime handle exceptions in a similar manner. Each language uses a form of try/catch/finally structured exception handling. This section provides several examples of basic exception handling.
In This Section
How to: Use the Try/Catch Block to Catch Exceptions
Describes how to use the try/catch block to handle exceptions.How to: Use Specific Exceptions in a Catch Block
Describes how to catch specific exceptions.How to: Create User-Defined Exceptions
Describes how to create your own exception classes.User-Filtered Exception Handlers
Describes how to set up filtered exceptions.How to: Use Finally Blocks
Explains how to use the finally statement in an exception block.
Related Sections
Handling and Throwing Exceptions
Provides an overview of common language runtime exceptions.Exception Class and Properties
Describes the elements of an exception object.