CodeTryCatchFinallyStatementとは? わかりやすく解説

CodeTryCatchFinallyStatement クラス

任意の数の catch 句と、省略可能な finally ブロックを持つ try ブロック表します

名前空間: System.CodeDom
アセンブリ: System (system.dll 内)
構文構文

<SerializableAttribute> _
<ComVisibleAttribute(True)> _
<ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _
Public Class CodeTryCatchFinallyStatement
    Inherits CodeStatement
Dim instance As CodeTryCatchFinallyStatement
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)] 
public class CodeTryCatchFinallyStatement :
 CodeStatement
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType::AutoDispatch)] 
public ref class CodeTryCatchFinallyStatement
 : public CodeStatement
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */ 
public class CodeTryCatchFinallyStatement extends
 CodeStatement
SerializableAttribute 
ComVisibleAttribute(true) 
ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) 
public class CodeTryCatchFinallyStatement extends
 CodeStatement
解説解説

CodeTryCatchFinallyStatement使用してコードtry/catch ブロックを表すことができます

TryStatements プロパティは、try ブロック内で実行するステートメント格納します。CatchClauses プロパティは、キャッチした例外処理する catch 句を格納します。FinallyStatements プロパティは、finally ブロック内で実行するステートメント格納します

メモメモ

try/catch ブロックは、すべての言語サポートされているわけではありません。コード ジェネレータtry/catch の各ブロックサポートしているかどうか調べるには、GeneratorSupport.TryCatchStatements フラグ指定して Supports メソッド呼び出します。

使用例使用例

CodeTryCatchFinallyStatement使用して例外処理するための try...catch...finally ステートメント定義する方法については、次のコード例参照してください

' 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");
'        }
'    }
// 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.
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.set_Name("ThrowApplicationException");
method1.get_Statements().Add(new 
    CodeThrowExceptionStatement(new CodeObjectCreateExpression(
    "System.ApplicationException", new CodeExpression[]
 { 
    new CodePrimitiveExpression("Test Application Exception")
 })));
type1.get_Members().Add(method1);
// Defines a constructor that calls the ThrowApplicationException 
// method from a try block.
CodeConstructor constructor1 = new CodeConstructor();
constructor1.set_Attributes(MemberAttributes.Public);
type1.get_Members().Add(constructor1);
// Defines a try statement that calls the 
// ThrowApplicationException method.
CodeTryCatchFinallyStatement try1 = new CodeTryCatchFinallyStatement();
try1.get_TryStatements().Add(new CodeMethodInvokeExpression(new
 
    CodeThisReferenceExpression(), "ThrowApplicationException", 
    new CodeExpression[] {}));
constructor1.get_Statements().Add(try1);
// Defines a catch clause for exceptions of type ApplicationException.
CodeCatchClause catch1 = new CodeCatchClause("ex", new
 
    CodeTypeReference("System.ApplicationException"));
catch1.get_Statements().Add(new CodeCommentStatement(
    "Handle any System.ApplicationException here."));
try1.get_CatchClauses().Add(catch1);
// Defines a catch clause for any remaining unhandled exception types.
CodeCatchClause catch2 = new CodeCatchClause("ex");
catch2.get_Statements().Add(new CodeCommentStatement(
    "Handle any other exception type here."));
try1.get_CatchClauses().Add(catch2);
// Defines a finally block by adding to the FinallyStatements collection.
try1.get_FinallyStatements().Add(new CodeCommentStatement(
    "Handle any finally block statements."));
// A VJ# 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");
//        }
//    }
継承階層継承階層
System.Object
   System.CodeDom.CodeObject
     System.CodeDom.CodeStatement
      System.CodeDom.CodeTryCatchFinallyStatement
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

CodeTryCatchFinallyStatement コンストラクタ ()


CodeTryCatchFinallyStatement コンストラクタ (CodeStatement[], CodeCatchClause[], CodeStatement[])

try 句、catch 句、finally ステートメントなどのステートメント指定して、CodeTryCatchFinallyStatement クラス新しインスタンス初期化します。

名前空間: System.CodeDom
アセンブリ: System (system.dll 内)
構文構文

Public Sub New ( _
    tryStatements As CodeStatement(), _
    catchClauses As CodeCatchClause(), _
    finallyStatements As CodeStatement() _
)
Dim tryStatements As CodeStatement()
Dim catchClauses As CodeCatchClause()
Dim finallyStatements As CodeStatement()

Dim instance As New CodeTryCatchFinallyStatement(tryStatements,
 catchClauses, finallyStatements)
public CodeTryCatchFinallyStatement (
    CodeStatement[] tryStatements,
    CodeCatchClause[] catchClauses,
    CodeStatement[] finallyStatements
)
public:
CodeTryCatchFinallyStatement (
    array<CodeStatement^>^ tryStatements, 
    array<CodeCatchClause^>^ catchClauses, 
    array<CodeStatement^>^ finallyStatements
)
public CodeTryCatchFinallyStatement (
    CodeStatement[] tryStatements, 
    CodeCatchClause[] catchClauses, 
    CodeStatement[] finallyStatements
)
public function CodeTryCatchFinallyStatement
 (
    tryStatements : CodeStatement[], 
    catchClauses : CodeCatchClause[], 
    finallyStatements : CodeStatement[]
)

パラメータ

tryStatements

トライするステートメントを示す CodeStatement オブジェクト配列

catchClauses

キャッチする句を示す CodeCatchClause オブジェクト配列

finallyStatements

使用する finally ステートメントを示す CodeStatement オブジェクト配列

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
CodeTryCatchFinallyStatement クラス
CodeTryCatchFinallyStatement メンバ
System.CodeDom 名前空間

CodeTryCatchFinallyStatement コンストラクタ (CodeStatement[], CodeCatchClause[])

try 句および catch 句のステートメント指定して、CodeTryCatchFinallyStatement クラス新しインスタンス初期化します。

名前空間: System.CodeDom
アセンブリ: System (system.dll 内)
構文構文

Public Sub New ( _
    tryStatements As CodeStatement(), _
    catchClauses As CodeCatchClause() _
)
Dim tryStatements As CodeStatement()
Dim catchClauses As CodeCatchClause()

Dim instance As New CodeTryCatchFinallyStatement(tryStatements,
 catchClauses)
public CodeTryCatchFinallyStatement (
    CodeStatement[] tryStatements,
    CodeCatchClause[] catchClauses
)
public:
CodeTryCatchFinallyStatement (
    array<CodeStatement^>^ tryStatements, 
    array<CodeCatchClause^>^ catchClauses
)
public CodeTryCatchFinallyStatement (
    CodeStatement[] tryStatements, 
    CodeCatchClause[] catchClauses
)
public function CodeTryCatchFinallyStatement
 (
    tryStatements : CodeStatement[], 
    catchClauses : CodeCatchClause[]
)

パラメータ

tryStatements

トライするステートメントを示す CodeStatement オブジェクト配列

catchClauses

キャッチする句を示す CodeCatchClause オブジェクト配列

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
CodeTryCatchFinallyStatement クラス
CodeTryCatchFinallyStatement メンバ
System.CodeDom 名前空間

CodeTryCatchFinallyStatement コンストラクタ

CodeTryCatchFinallyStatement クラス新しインスタンス初期化します。
オーバーロードの一覧オーバーロードの一覧

名前 説明
CodeTryCatchFinallyStatement () CodeTryCatchFinallyStatement クラス新しインスタンス初期化します。
CodeTryCatchFinallyStatement (CodeStatement[], CodeCatchClause[]) try 句および catch 句のステートメント指定してCodeTryCatchFinallyStatement クラス新しインスタンス初期化します。
CodeTryCatchFinallyStatement (CodeStatement[], CodeCatchClause[], CodeStatement[]) try 句、catch 句、finally ステートメントなどのステートメント指定してCodeTryCatchFinallyStatement クラス新しインスタンス初期化します。
参照参照

関連項目

CodeTryCatchFinallyStatement クラス
CodeTryCatchFinallyStatement メンバ
System.CodeDom 名前空間

CodeTryCatchFinallyStatement プロパティ


パブリック プロパティパブリック プロパティ

  名前 説明
パブリック プロパティ CatchClauses 使用する catch 句を取得します
パブリック プロパティ EndDirectives  終了ディレクティブを含む CodeDirectiveCollection オブジェクト取得します。 ( CodeStatement から継承されます。)
パブリック プロパティ FinallyStatements 使用する finally ステートメント取得します
パブリック プロパティ LinePragma  コード ステートメントのある行を取得または設定します。 ( CodeStatement から継承されます。)
パブリック プロパティ StartDirectives  開始ディレクティブを含む CodeDirectiveCollection オブジェクト取得します。 ( CodeStatement から継承されます。)
パブリック プロパティ TryStatements トライするステートメント取得します
パブリック プロパティ UserData  現在のオブジェクトユーザー定義可能なデータ取得または設定します。 ( CodeObject から継承されます。)
参照参照

関連項目

CodeTryCatchFinallyStatement クラス
System.CodeDom 名前空間

CodeTryCatchFinallyStatement メソッド


CodeTryCatchFinallyStatement メンバ

任意の数の catch 句と、省略可能な finally ブロックを持つ try ブロック表します

CodeTryCatchFinallyStatement データ型公開されるメンバを以下の表に示します


パブリック コンストラクタパブリック コンストラクタ
  名前 説明
パブリック メソッド CodeTryCatchFinallyStatement オーバーロードされます。 CodeTryCatchFinallyStatement クラス新しインスタンス初期化します。
パブリック プロパティパブリック プロパティ
  名前 説明
パブリック プロパティ CatchClauses 使用する catch 句を取得します
パブリック プロパティ EndDirectives  終了ディレクティブを含む CodeDirectiveCollection オブジェクト取得します。(CodeStatement から継承されます。)
パブリック プロパティ FinallyStatements 使用する finally ステートメント取得します
パブリック プロパティ LinePragma  コード ステートメントのある行を取得または設定します。 (CodeStatement から継承されます。)
パブリック プロパティ StartDirectives  開始ディレクティブを含む CodeDirectiveCollection オブジェクト取得します。(CodeStatement から継承されます。)
パブリック プロパティ TryStatements トライするステートメント取得します
パブリック プロパティ UserData  現在のオブジェクトユーザー定義可能なデータ取得または設定します。(CodeObject から継承されます。)
パブリック メソッドパブリック メソッド
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

CodeTryCatchFinallyStatement クラス
System.CodeDom 名前空間


このページでは「.NET Framework クラス ライブラリ リファレンス」からCodeTryCatchFinallyStatementを検索した結果を表示しています。
Weblioに収録されているすべての辞書からCodeTryCatchFinallyStatementを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からCodeTryCatchFinallyStatement を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「CodeTryCatchFinallyStatement」の関連用語

CodeTryCatchFinallyStatementのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



CodeTryCatchFinallyStatementのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS