ExceptionHandlingClause::HandlerLength Property
Gets the length, in bytes, of the body of this exception-handling clause.
Assembly: mscorlib (in mscorlib.dll)
Property Value
Type: System::Int32An integer that represents the length, in bytes, of the MSIL that forms the body of this exception-handling clause.
Note |
|---|
Working with exception-handling clauses requires a thorough understanding of metadata and Microsoft intermediate language (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; see ECMA C# and Common Language Infrastructure Standards on MSDN and Standard ECMA-335 - Common Language Infrastructure (CLI) on the Ecma International Web site. |
The following code example defines a test method named MethodBodyExample, and displays its local variable information and 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 and display their properties.
Note |
|---|
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 is part of a larger example located in the MethodBody class topic.
#using <System.dll> using namespace System; using namespace System::Reflection; public ref class Example { // 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 == nullptr) { throw gcnew ArgumentNullException("The argument cannot " + "be null."); } if (arg->GetType() == String::typeid) { throw gcnew 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 (ArgumentException^ ex) { Console::WriteLine("Ordinary exception-handling clause caught:" + " {0}", ex->GetType()); } finally { var1 = 3033; var2 = "Another string."; } } }; int main() { // Get method body information. MethodInfo^ mi = Example::typeid->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(); for each(ExceptionHandlingClause^ exhc in mb->ExceptionHandlingClauses) { Console::WriteLine(exhc->Flags.ToString()); // The FilterOffset property is meaningful only for Filter // clauses. The CatchType property is not meaningful for // Filter or Finally clauses. switch(exhc->Flags) { case ExceptionHandlingClauseOptions::Filter: Console::WriteLine(" Filter Offset: {0}", exhc->FilterOffset); break; case ExceptionHandlingClauseOptions::Finally: break; default: Console::WriteLine(" Type of exception: {0}", exhc->CatchType); break; } Console::WriteLine(" Handler Length: {0}", exhc->HandlerLength); Console::WriteLine(" Handler Offset: {0}", exhc->HandlerOffset); Console::WriteLine(" Try Block Length: {0}", exhc->TryLength); Console::WriteLine(" Try Block Offset: {0}", exhc->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 == nullptr)
{
throw gcnew ArgumentNullException("The argument cannot " +
"be null.");
}
if (arg->GetType() == String::typeid)
{
throw gcnew 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 (ArgumentException^ ex)
{
Console::WriteLine("Ordinary exception-handling clause caught:" +
" {0}", ex->GetType());
}
finally
{
var1 = 3033;
var2 = "Another string.";
}
}
Available since 2.0
