ExceptionHandlingClauses Property

MethodBody.ExceptionHandlingClauses Property

Note: This property is new in the .NET Framework version 2.0.

Gets a list that includes all the exception-handling clauses in the method body.

Namespace: System.Reflection
Assembly: mscorlib (in mscorlib.dll)

public IList<ExceptionHandlingClause> ExceptionHandlingClauses { get; }
/** @property */
public IList<ExceptionHandlingClause> get_ExceptionHandlingClauses ()

public function get ExceptionHandlingClauses () : IList<ExceptionHandlingClause>

Property Value

An IList of ExceptionHandlingClause objects representing the exception-handling clauses in the body of the method.

The list includes all clauses from all try blocks in the method body. Each ExceptionHandlingClause object contains information that identifies the try block with which it is associated.


Working with exception-handling clauses requires a thorough understanding of metadata and MSIL instruction formats. Information can be found in the Common Language Infrastructure (CLI) documentation, especially "Partition II: Metadata Definition and Semantics" and "Partition III: CIL Instruction Set". The documentation is available online at http://msdn.microsoft.com/net/ecma/ and http://www.ecma-international.org/publications/standards/Ecma-335.htm.

The following code example defines a test method named MethodBodyExample and displays information about its exception-handling clauses. The MethodBase.GetMethodBody method is used to obtain a MethodBody object for the test method. The ExceptionHandlingClauses property is used to obtain a list of ExceptionHandlingClause objects.


Not all computer languages can generate ExceptionHandlingClauseOptions.Filter clauses. The Visual Basic example shows a filter clause, using a Visual Basic When expression, which is omitted from the examples for other languages.

This code example is part of a larger example provided for the MethodBody class.

using System;
using System.Reflection;

public class Example
    public static void Main()
        // Get method body information.
        MethodInfo mi = typeof(Example).GetMethod("MethodBodyExample");
        MethodBody mb = mi.GetMethodBody();
        Console.WriteLine("\r\nMethod: {0}", mi);

        // Display the general information included in the 
        // MethodBody object.
        Console.WriteLine("    Local variables are initialized: {0}", 
        Console.WriteLine("    Maximum number of items on the operand stack: {0}", 
        // Display exception handling clauses.
        foreach (ExceptionHandlingClause ehc in mb.ExceptionHandlingClauses)

            // The FilterOffset property is meaningful only for Filter
            // clauses. The CatchType property is not meaningful for 
            // Filter or Finally clauses. 
            switch (ehc.Flags)
                case ExceptionHandlingClauseOptions.Filter:
                    Console.WriteLine("        Filter Offset: {0}", 
                case ExceptionHandlingClauseOptions.Finally:
                    Console.WriteLine("    Type of exception: {0}", 

            Console.WriteLine("       Handler Length: {0}", ehc.HandlerLength);
            Console.WriteLine("       Handler Offset: {0}", ehc.HandlerOffset);
            Console.WriteLine("     Try Block Length: {0}", ehc.TryLength);
            Console.WriteLine("     Try Block Offset: {0}", ehc.TryOffset);
...    }

    // The Main method contains code to analyze this method, using
    // the properties and methods of the MethodBody class.
    public void MethodBodyExample(object arg)
        // Define some local variables. In addition to these variables,
        // the local variable list includes the variables scoped to 
        // the catch clauses.
        int var1 = 42;
        string var2 = "Forty-two";

            // Depending on the input value, throw an ArgumentException or 
            // an ArgumentNullException to test the Catch clauses.
            if (arg == null)
                throw new ArgumentNullException("The argument cannot be null.");
            if (arg.GetType() == typeof(string))
                throw new ArgumentException("The argument cannot be a string.");

        // There is no Filter clause in this code example. See the Visual 
        // Basic code for an example of a Filter clause.

        // This catch clause handles the ArgumentException class, and
        // any other class derived from Exception.
        catch(Exception ex)
            Console.WriteLine("Ordinary exception-handling clause caught: {0}", 
            var1 = 3033;
            var2 = "Another string.";

// This code example produces output similar to the following:
//Method: Void MethodBodyExample(System.Object)
//    Local variables are initialized: True
//    Maximum number of items on the operand stack: 2
//    Type of exception: System.Exception
//       Handler Length: 21
//       Handler Offset: 70
//     Try Block Length: 61
//     Try Block Offset: 9
//       Handler Length: 14
//       Handler Offset: 94
//     Try Block Length: 85
//     Try Block Offset: 9

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions

© 2015 Microsoft