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 は、try/catch ステートメントの 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. Statements collection プロパティには、catch ブロックのステートメントが含まれています。The 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()

現在のインスタンスの Type を取得します。Gets the Type of the current instance.

(継承元 Object)
MemberwiseClone()

現在の Object の簡易コピーを作成します。Creates a shallow copy of the current Object.

(継承元 Object)
ToString()

現在のオブジェクトを表す string を返します。Returns a string that represents the current object.

(継承元 Object)

適用対象

こちらもご覧ください