CodeCatchClause 类

定义

表示 catch 语句的 try/catch 异常块。Represents a catch exception block of a try/catch statement.

public ref class CodeCatchClause
[System.Runtime.InteropServices.ComVisible(true)]
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)]
[System.Serializable]
public class CodeCatchClause
type CodeCatchClause = class
Public Class CodeCatchClause
继承
CodeCatchClause
属性

示例

下面的示例代码演示了CodeCatchClause如何使用对象定义 try .。。catch 块。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");
//        }
//    }
        // 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

注解

CodeCatchClause可用于表示catch try/catch语句的异常块。CodeCatchClause can be used to represent a catch exception block of a try/catch statement.

CatchExceptionType属性指定要捕获的异常的类型。The CatchExceptionType property specifies the type of exception to catch. LocalName属性为表示已捕获的异常的变量指定名称。The LocalName property specifies a name for the variable representing the exception that has been caught. 集合属性包含catch块的语句。 StatementsThe Statements collection property contains the statements for the catch block.

构造函数

CodeCatchClause()

初始化 CodeCatchClause 类的新实例。Initializes a new instance of the CodeCatchClause class.

CodeCatchClause(String)

使用异常的指定局部变量名初始化 CodeCatchClause 类的新实例。Initializes a new instance of the CodeCatchClause class using the specified local variable name for the exception.

CodeCatchClause(String, CodeTypeReference)

使用异常的指定局部变量名和异常类型初始化 CodeCatchClause 类的新实例。Initializes a new instance of the CodeCatchClause class using the specified local variable name for the exception and exception type.

CodeCatchClause(String, CodeTypeReference, CodeStatement[])

使用异常的指定局部变量名、异常类型和语句集合初始化 CodeCatchClause 类的新实例。Initializes a new instance of the CodeCatchClause class using the specified local variable name for the exception, exception type and statement collection.

属性

CatchExceptionType

获取或设置处理 catch 块的异常的类型。Gets or sets the type of the exception to handle with the catch block.

LocalName

获取或设置 catch 子句处理的异常的变量名称。Gets or sets the variable name of the exception that the catch clause handles.

Statements

获取 catch 块中的语句。Gets the statements within the catch block.

方法

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

适用于

另请参阅