CodeTryCatchFinallyStatement Class


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

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

Namespace:   System.CodeDom
Assembly:  System (in System.dll)


Public Class CodeTryCatchFinallyStatement
	Inherits CodeStatement


Initializes a new instance of the CodeTryCatchFinallyStatement class.

System_CAPS_pubmethodCodeTryCatchFinallyStatement(CodeStatement(), CodeCatchClause())

Initializes a new instance of the CodeTryCatchFinallyStatement class using the specified statements for try and catch clauses.

System_CAPS_pubmethodCodeTryCatchFinallyStatement(CodeStatement(), CodeCatchClause(), CodeStatement())

Initializes a new instance of the CodeTryCatchFinallyStatement class using the specified statements for try, catch clauses, and finally statements.


Gets the catch clauses to use.


Gets a CodeDirectiveCollection object that contains end directives.(Inherited from CodeStatement.)


Gets the finally statements to use.


Gets or sets the line on which the code statement occurs. (Inherited from CodeStatement.)


Gets a CodeDirectiveCollection object that contains start directives.(Inherited from CodeStatement.)


Gets the statements to try.


Gets the user-definable data for the current object.(Inherited from CodeObject.)


Determines whether the specified object is equal to the current object.(Inherited from Object.)


Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)


Serves as the default hash function. (Inherited from Object.)


Gets the Type of the current instance.(Inherited from Object.)


Creates a shallow copy of the current Object.(Inherited from Object.)


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.


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.
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"))))

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

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

' 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."))

' 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."))

' 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 Visual Basic source 
' code for the preceeding example code:

'            '------------------------------------------------------------------------------
'            ' <auto-generated>
'            '     This code was generated by a tool.
'            '     Runtime Version:2.0.50727.42
'            '
'            '     Changes to this file may cause incorrect behavior and will be lost if
'            '     the code is regenerated.
'            ' </auto-generated>
'            '------------------------------------------------------------------------------

'Option Strict Off
'Option Explicit On

'Imports System

'Namespace Samples

'            Public Class TryCatchTest

'                Public Sub New()
'                    MyBase.New()
'                    Try
'                        Me.ThrowApplicationException()
'                    Catch ex As System.ApplicationException
'                        'Handle any System.ApplicationException here.
'                    Catch ex As System.Exception
'                        'Handle any other exception type here.
'                    Finally
'                        'Handle any finally block statements.
'                    End Try
'                End Sub

'                Private Sub ThrowApplicationException()
'                    Throw New System.ApplicationException("Test Application Exception")
'                End Sub
'            End Class
'        End Namespace

.NET Framework
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.

Return to top