CodeCatchClause Class
Represents a catch exception block of a try/catch statement.
Assembly: System (in System.dll)
| Name | Description | |
|---|---|---|
![]() | CodeCatchClause() | Initializes a new instance of the CodeCatchClause class. |
![]() | CodeCatchClause(String^) | Initializes a new instance of the CodeCatchClause class using the specified local variable name for the exception. |
![]() | CodeCatchClause(String^, CodeTypeReference^) | Initializes a new instance of the CodeCatchClause class using the specified local variable name for the exception and exception type. |
![]() | CodeCatchClause(String^, CodeTypeReference^, array<CodeStatement^>^) | Initializes a new instance of the CodeCatchClause class using the specified local variable name for the exception, exception type and statement collection. |
| Name | Description | |
|---|---|---|
![]() | CatchExceptionType | Gets or sets the type of the exception to handle with the catch block. |
![]() | LocalName | Gets or sets the variable name of the exception that the catch clause handles. |
![]() | Statements | Gets the statements within the catch block. |
| Name | Description | |
|---|---|---|
![]() | Equals(Object^) | Determines whether the specified object is equal to the current object.(Inherited from Object.) |
![]() | Finalize() | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.) |
![]() | GetHashCode() | Serves as the default hash function. (Inherited from Object.) |
![]() | GetType() | |
![]() | MemberwiseClone() | |
![]() | ToString() | Returns a string that represents the current object.(Inherited from Object.) |
CodeCatchClause can be used to represent a catch exception block of a try/catch statement.
The CatchExceptionType property specifies the type of exception to catch. The LocalName property specifies a name for the variable representing the exception that has been caught. The Statements collection property contains the statements for the catch block.
The following example code demonstrates using CodeCatchClause objects to define exception handling clauses of a try...catch block.
// Declares a type to contain a try...catch block. CodeTypeDeclaration^ type1 = gcnew CodeTypeDeclaration( "TryCatchTest" ); // Defines a method that throws an exception of type System.ApplicationException. CodeMemberMethod^ method1 = gcnew CodeMemberMethod; method1->Name = "ThrowApplicationException"; array<CodePrimitiveExpression^>^temp = {gcnew CodePrimitiveExpression( "Test Application Exception" )}; method1->Statements->Add( gcnew CodeThrowExceptionStatement( gcnew CodeObjectCreateExpression( "System.ApplicationException",temp ) ) ); type1->Members->Add( method1 ); // Defines a constructor that calls the ThrowApplicationException method from a try block. CodeConstructor^ constructor1 = gcnew CodeConstructor; constructor1->Attributes = MemberAttributes::Public; type1->Members->Add( constructor1 ); // Defines a try statement that calls the ThrowApplicationException method. CodeTryCatchFinallyStatement^ try1 = gcnew CodeTryCatchFinallyStatement; try1->TryStatements->Add( gcnew CodeMethodInvokeExpression( gcnew CodeThisReferenceExpression,"ThrowApplicationException", nullptr ) ); constructor1->Statements->Add( try1 ); // Defines a catch clause for exceptions of type ApplicationException. CodeCatchClause^ catch1 = gcnew CodeCatchClause( "ex",gcnew CodeTypeReference( "System.ApplicationException" ) ); catch1->Statements->Add( gcnew CodeCommentStatement( "Handle any System.ApplicationException here." ) ); try1->CatchClauses->Add( catch1 ); // Defines a catch clause for any remaining unhandled exception types. CodeCatchClause^ catch2 = gcnew CodeCatchClause( "ex" ); catch2->Statements->Add( gcnew CodeCommentStatement( "Handle any other exception type here." ) ); try1->CatchClauses->Add( catch2 ); // Defines a finally block by adding to the FinallyStatements collection. try1->FinallyStatements->Add( gcnew CodeCommentStatement( "Handle any finally block statements." ) ); // A C# code generator produces the following source code for the preceeding example code: // public class TryCatchTest // { // // public TryCatchTest() // { // try // { // this.ThrowApplicationException(); // } // catch (System.ApplicationException ex) // { // // Handle any System.ApplicationException here. // } // catch (System.Exception ex) // { // // Handle any other exception type here. // } // finally { // // Handle any finally block statements. // } // } // // private void ThrowApplicationException() // { // throw new System.ApplicationException("Test Application Exception"); // } // }
Available since 1.1
Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.


