CodeTryCatchFinallyStatement Class
Represents a try block with any number of catch clauses and, optionally, a finally block.
System.CodeDom::CodeObject
System.CodeDom::CodeStatement
System.CodeDom::CodeTryCatchFinallyStatement
Assembly: System (in System.dll)
The CodeTryCatchFinallyStatement type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | CodeTryCatchFinallyStatement() | Initializes a new instance of the CodeTryCatchFinallyStatement class. |
![]() | CodeTryCatchFinallyStatement(array<CodeStatement>, array<CodeCatchClause>) | Initializes a new instance of the CodeTryCatchFinallyStatement class using the specified statements for try and catch clauses. |
![]() | CodeTryCatchFinallyStatement(array<CodeStatement>, array<CodeCatchClause>, array<CodeStatement>) | Initializes a new instance of the CodeTryCatchFinallyStatement class using the specified statements for try, catch clauses, and finally statements. |
| Name | Description | |
|---|---|---|
![]() | CatchClauses | Gets the catch clauses to use. |
![]() | EndDirectives | Gets a CodeDirectiveCollection object that contains end directives. (Inherited from CodeStatement.) |
![]() | FinallyStatements | Gets the finally statements to use. |
![]() | LinePragma | Gets or sets the line on which the code statement occurs. (Inherited from CodeStatement.) |
![]() | StartDirectives | Gets a CodeDirectiveCollection object that contains start directives. (Inherited from CodeStatement.) |
![]() | TryStatements | Gets the statements to try. |
![]() | UserData | Gets the user-definable data for the current object. (Inherited from CodeObject.) |
| 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 a hash function for a particular type. (Inherited from Object.) |
![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() | ToString | Returns a string that represents the current object. (Inherited from Object.) |
CodeTryCatchFinallyStatement can be used to represent a try/ catch block of code.
The TryStatements property contains the statements to execute within a try block. The CatchClauses property contains the catch clauses to handle caught exceptions. The FinallyStatements property contains the statements to execute within a finally block.
Note |
|---|
Not all languages support try/catch blocks. Call the Supports method with the GeneratorSupport::TryCatchStatements flag to determine whether a code generator supports try/catch blocks. |
The following example code demonstrates use of a CodeTryCatchFinallyStatement to define a try...catch...finally statement for exception handling.
// 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"); // } // }
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
