CodeTryCatchFinallyStatement CodeTryCatchFinallyStatement CodeTryCatchFinallyStatement CodeTryCatchFinallyStatement Class

定義

表示 try 區塊和任意數目的 catch 子句,亦可選擇 finally 區塊。Represents a try block with any number of catch clauses and, optionally, a finally block.

public ref class CodeTryCatchFinallyStatement : System::CodeDom::CodeStatement
[System.Runtime.InteropServices.ComVisible(true)]
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)]
[System.Serializable]
public class CodeTryCatchFinallyStatement : System.CodeDom.CodeStatement
type CodeTryCatchFinallyStatement = class
    inherit CodeStatement
Public Class CodeTryCatchFinallyStatement
Inherits CodeStatement
繼承
CodeTryCatchFinallyStatementCodeTryCatchFinallyStatementCodeTryCatchFinallyStatementCodeTryCatchFinallyStatement
屬性

範例

下列範例程式碼示範CodeTryCatchFinallyStatement如何使用來定義 try .。。catch .。。例外狀況處理的 finally 語句。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");
//        }
//    }
        // 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");
        //        }
        //    }

' 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 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

'            '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

備註

CodeTryCatchFinallyStatement可以用來表示try /程式碼catch區塊。CodeTryCatchFinallyStatement can be used to represent a try/catch block of code.

屬性包含要try在區塊內執行的語句。 TryStatementsThe TryStatements property contains the statements to execute within a try block. 屬性包含用來catch處理攔截到例外狀況的子句。 CatchClausesThe CatchClauses property contains the catch clauses to handle caught exceptions. 屬性包含要finally在區塊內執行的語句。 FinallyStatementsThe FinallyStatements property contains the statements to execute within a finally block.

注意

並非所有語言都try支援/ catch區塊。Not all languages support try/catch blocks. catch / try使用旗標呼叫方法,以判斷程式碼產生器是否支援區塊。Supports GeneratorSupport.TryCatchStatementsCall the Supports method with the GeneratorSupport.TryCatchStatements flag to determine whether a code generator supports try/catch blocks.

建構函式

CodeTryCatchFinallyStatement() CodeTryCatchFinallyStatement() CodeTryCatchFinallyStatement() CodeTryCatchFinallyStatement()

初始化 CodeTryCatchFinallyStatement 類別的新執行個體。Initializes a new instance of the CodeTryCatchFinallyStatement class.

CodeTryCatchFinallyStatement(CodeStatement[], CodeCatchClause[]) CodeTryCatchFinallyStatement(CodeStatement[], CodeCatchClause[]) CodeTryCatchFinallyStatement(CodeStatement[], CodeCatchClause[]) CodeTryCatchFinallyStatement(CodeStatement[], CodeCatchClause[])

使用指定的 Try 和 Catch 子句陳述式來初始化 CodeTryCatchFinallyStatement 類別的新執行個體。Initializes a new instance of the CodeTryCatchFinallyStatement class using the specified statements for try and catch clauses.

CodeTryCatchFinallyStatement(CodeStatement[], CodeCatchClause[], CodeStatement[]) CodeTryCatchFinallyStatement(CodeStatement[], CodeCatchClause[], CodeStatement[]) CodeTryCatchFinallyStatement(CodeStatement[], CodeCatchClause[], CodeStatement[]) CodeTryCatchFinallyStatement(CodeStatement[], CodeCatchClause[], CodeStatement[])

使用指定的 Try 陳述式、Catch 子句和 Finally 陳述式來初始化 CodeTryCatchFinallyStatement 類別的新執行個體。Initializes a new instance of the CodeTryCatchFinallyStatement class using the specified statements for try, catch clauses, and finally statements.

屬性

CatchClauses CatchClauses CatchClauses CatchClauses

取得要使用的 Catch 子句。Gets the catch clauses to use.

EndDirectives EndDirectives EndDirectives EndDirectives

取得包含結尾指示詞的 CodeDirectiveCollection 物件。Gets a CodeDirectiveCollection object that contains end directives.

(Inherited from CodeStatement)
FinallyStatements FinallyStatements FinallyStatements FinallyStatements

取得要使用的 Finally 陳述式 (Statement)。Gets the finally statements to use.

LinePragma LinePragma LinePragma LinePragma

取得或設定程式碼陳述式發生所在的行。Gets or sets the line on which the code statement occurs.

(Inherited from CodeStatement)
StartDirectives StartDirectives StartDirectives StartDirectives

取得包含開頭指示詞的 CodeDirectiveCollection 物件。Gets a CodeDirectiveCollection object that contains start directives.

(Inherited from CodeStatement)
TryStatements TryStatements TryStatements TryStatements

取得要嘗試的陳述式。Gets the statements to try.

UserData UserData UserData UserData

取得目前物件的使用者可定義資料。Gets the user-definable data for the current object.

(Inherited from CodeObject)

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

適用於