Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

CodeTryCatchFinallyStatement Class

Represents a try block with any number of catch clauses and, optionally, a finally block.

For a list of all members of this type, see CodeTryCatchFinallyStatement Members.

System.Object
   System.CodeDom.CodeObject
      System.CodeDom.CodeStatement
         System.CodeDom.CodeTryCatchFinallyStatement

[Visual Basic]
<Serializable>
<ClassInterface(ClassInterfaceType.AutoDispatch)>
<ComVisible(True)>
Public Class CodeTryCatchFinallyStatement
   Inherits CodeStatement
[C#]
[Serializable]
[ClassInterface(ClassInterfaceType.AutoDispatch)]
[ComVisible(true)]
public class CodeTryCatchFinallyStatement : CodeStatement
[C++]
[Serializable]
[ClassInterface(ClassInterfaceType::AutoDispatch)]
[ComVisible(true)]
public __gc class CodeTryCatchFinallyStatement : public
   CodeStatement
[JScript]
public
   Serializable
 ClassInterface(ClassInterfaceType.AutoDispatch)
 ComVisible(true)
class CodeTryCatchFinallyStatement extends CodeStatement

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

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.

Example

[Visual Basic, C#, C++] The following example code demonstrates use of a CodeTryCatchFinallyStatement to define a try...catch...finally statement for exception handling.

[Visual Basic] 
' Declares a type to contain a try...catch block.
Dim type1 As New CodeTypeDeclaration("TryCatchTest")

' Defines a method that throws an exception of type System.ApplicationException.
Dim method1 As New CodeMemberMethod()
method1.Name = "ThrowApplicationException"
method1.Statements.Add(New CodeThrowExceptionStatement( _
    New CodeObjectCreateExpression("System.ApplicationException", New CodePrimitiveExpression("Test Application Exception"))))
type1.Members.Add(method1)

' Defines a constructor that calls the ThrowApplicationException method from a try block.
Dim constructor1 As New CodeConstructor()
constructor1.Attributes = MemberAttributes.Public
type1.Members.Add(constructor1)

' Defines a try statement that calls the ThrowApplicationException method.
Dim try1 As New CodeTryCatchFinallyStatement()
try1.TryStatements.Add(New CodeMethodInvokeExpression(New CodeThisReferenceExpression(), "ThrowApplicationException"))
constructor1.Statements.Add(try1)

' Defines a catch clause for exceptions of type ApplicationException.
Dim catch1 As New CodeCatchClause("ex", New CodeTypeReference("System.ApplicationException"))
catch1.Statements.Add(New CodeCommentStatement("Handle any System.ApplicationException here."))
try1.CatchClauses.Add(catch1)

' Defines a catch clause for any remaining unhandled exception types.
Dim catch2 As New CodeCatchClause("ex")
catch2.Statements.Add(New CodeCommentStatement("Handle any other exception type here."))
try1.CatchClauses.Add(catch2)

' Defines a finally block by adding to the FinallyStatements collection.
try1.FinallyStatements.Add(New CodeCommentStatement("Handle any finally block statements."))

' A Visual Basic 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");
'        }
'    }

[C#] 
// Declares a type to contain a try...catch block.
CodeTypeDeclaration type1 = new CodeTypeDeclaration("TryCatchTest");

// Defines a method that throws an exception of type System.ApplicationException.
CodeMemberMethod method1 = new CodeMemberMethod();
method1.Name = "ThrowApplicationException";
method1.Statements.Add( new CodeThrowExceptionStatement( 
    new CodeObjectCreateExpression("System.ApplicationException", new CodePrimitiveExpression("Test Application Exception")) ) );
type1.Members.Add( method1 );

// Defines a constructor that calls the ThrowApplicationException method from a try block.
CodeConstructor constructor1 = new CodeConstructor();
constructor1.Attributes = MemberAttributes.Public;            
type1.Members.Add( constructor1 );    
        
// Defines a try statement that calls the ThrowApplicationException method.
CodeTryCatchFinallyStatement try1 = new CodeTryCatchFinallyStatement();
try1.TryStatements.Add( new CodeMethodInvokeExpression( new CodeThisReferenceExpression(), "ThrowApplicationException" ) );
constructor1.Statements.Add( try1 );                    

// Defines a catch clause for exceptions of type ApplicationException.
CodeCatchClause catch1 = new CodeCatchClause("ex", new CodeTypeReference("System.ApplicationException"));
catch1.Statements.Add( new CodeCommentStatement("Handle any System.ApplicationException here.") );
try1.CatchClauses.Add( catch1 );

// Defines a catch clause for any remaining unhandled exception types.
CodeCatchClause catch2 = new CodeCatchClause("ex");
catch2.Statements.Add( new CodeCommentStatement("Handle any other exception type here.") );
try1.CatchClauses.Add( catch2 );
        
// Defines a finally block by adding to the FinallyStatements collection.
try1.FinallyStatements.Add( new 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");
//        }
//    }


[C++] 
// Declares a type to contain a try...catch block.
CodeTypeDeclaration* type1 = new CodeTypeDeclaration(S"TryCatchTest");

// Defines a method that throws an exception of type System.ApplicationException.
CodeMemberMethod* method1 = new CodeMemberMethod();
method1->Name = S"ThrowApplicationException";
CodePrimitiveExpression* temp[] = {new CodePrimitiveExpression(S"Test Application Exception")};
method1->Statements->Add( new CodeThrowExceptionStatement( 
    new CodeObjectCreateExpression(S"System.ApplicationException", temp) ) );
type1->Members->Add( method1 );

// Defines a constructor that calls the ThrowApplicationException method from a try block.
CodeConstructor* constructor1 = new CodeConstructor();
constructor1->Attributes = MemberAttributes::Public;            
type1->Members->Add( constructor1 );    

// Defines a try statement that calls the ThrowApplicationException method.
CodeTryCatchFinallyStatement* try1 = new CodeTryCatchFinallyStatement();
try1->TryStatements->Add( new CodeMethodInvokeExpression( new CodeThisReferenceExpression(), S"ThrowApplicationException", 0 ) );
constructor1->Statements->Add( try1 );                    

// Defines a catch clause for exceptions of type ApplicationException.
CodeCatchClause* catch1 = new CodeCatchClause(S"ex", new CodeTypeReference(S"System.ApplicationException"));
catch1->Statements->Add( new CodeCommentStatement(S"Handle any System.ApplicationException here.") );
try1->CatchClauses->Add( catch1 );

// Defines a catch clause for any remaining unhandled exception types.
CodeCatchClause* catch2 = new CodeCatchClause(S"ex");
catch2->Statements->Add( new CodeCommentStatement(S"Handle any other exception type here.") );
try1->CatchClauses->Add( catch2 );

// Defines a finally block by adding to the FinallyStatements collection.
try1->FinallyStatements->Add( new CodeCommentStatement(S"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");
//        }
//    }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.CodeDom

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System (in System.dll)

See Also

CodeTryCatchFinallyStatement Members | System.CodeDom Namespace

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft