Ce sujet n'a pas encore été évalué - Évaluez ce sujet

ExceptionHandlingClause.TryOffset, propriété

Offset dans la méthode, en octets, du bloc try qui inclut cette clause de gestion des exceptions.

Espace de noms: System.Reflection
Assembly : mscorlib (dans mscorlib.dll)

public int TryOffset { get; }
/** @property */
public int get_TryOffset ()

public function get TryOffset () : int

Non applicable.

Valeur de la propriété

Entier qui représente l'offset dans la méthode, en octets, du bloc try qui inclut cette clause de gestion des exceptions.
RemarqueRemarque :

L'utilisation de clauses de gestion des exceptions nécessite une bonne connaissance des métadonnées et des formats d'instructions MSIL (Microsoft Intermediate Language). Vous trouverez des informations dans la documentation relative à l'infrastructure du langage commun (CLI), plus précisément dans « Partition II: Metadata Definition and Semantics » et « Partition III: CIL Instruction Set ». La documentation est disponible en ligne à l'adresse http://msdn.microsoft.com/net/ecma/ et http://www.ecma-international.org/publications/standards/Ecma-335.htm.

L'exemple de code suivant définit une méthode de test nommée MethodBodyExample et affiche ses informations de variable locale ainsi que ses clauses de gestion des exceptions. La méthode MethodBase.GetMethodBody est utilisée pour obtenir un objet MethodBody pour la méthode test. La propriété ExceptionHandlingClauses est utilisée pour obtenir une liste des objets ExceptionHandlingClause et pour afficher leurs propriétés.

RemarqueRemarque :

Tous les langages informatiques ne peuvent pas générer de clauses ExceptionHandlingClauseOptions.Filter. L'exemple Visual Basic montre une clause de filtre, utilisant une expression When Visual Basic, qui est omise des exemples relatifs aux autres langages.

Ce code fait partie d'un exemple plus développé disponible dans la rubrique consacrée à la classe MethodBody.

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}", 
            mb.InitLocals);
        Console.WriteLine("    Maximum number of items on the operand stack: {0}", 
            mb.MaxStackSize);
...


        // Display exception handling clauses.
        Console.WriteLine();
        foreach (ExceptionHandlingClause ehc in mb.ExceptionHandlingClauses)
        {
            Console.WriteLine(ehc.Flags.ToString());

            // 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}", 
                        ehc.FilterOffset);
                    break;
                case ExceptionHandlingClauseOptions.Finally:
                    break;
                default:
                    Console.WriteLine("    Type of exception: {0}", 
                        ehc.CatchType);
                    break;
            }

            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";

        try
        {
            // 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}", 
                ex.GetType());
        }        
        finally
        {
            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
...

//
//Clause
//    Type of exception: System.Exception
//       Handler Length: 21
//       Handler Offset: 70
//     Try Block Length: 61
//     Try Block Offset: 9
//Finally
//       Handler Length: 14
//       Handler Offset: 94
//     Try Block Length: 85
//     Try Block Offset: 9

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Édition Media Center, Windows XP Professionnel Édition x64, Windows XP SP2, Windows XP Starter Edition

Microsoft .NET Framework 3.0 est pris en charge sur Windows Vista, Microsoft Windows XP SP2 et Windows Server 2003 SP1.

.NET Framework

Prise en charge dans : 3.0, 2.0
Cela vous a-t-il été utile ?
(1500 caractères restants)

Ajouts de la communauté

AJOUTER
© 2013 Microsoft. Tous droits réservés.