CodeTryCatchFinallyStatement 클래스
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
여러 개의 try
절을 가진 catch
블록과 선택적으로 finally
블록을 나타냅니다.
public ref class CodeTryCatchFinallyStatement : System::CodeDom::CodeStatement
public class CodeTryCatchFinallyStatement : System.CodeDom.CodeStatement
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public class CodeTryCatchFinallyStatement : System.CodeDom.CodeStatement
type CodeTryCatchFinallyStatement = class
inherit CodeStatement
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
[<System.Serializable>]
type CodeTryCatchFinallyStatement = class
inherit CodeStatement
Public Class CodeTryCatchFinallyStatement
Inherits CodeStatement
- 상속
- 특성
예제
다음 예제 코드는 를 사용하여 CodeTryCatchFinallyStatement 예외 처리를 위한 문을 정의하는 try...catch...finally
방법을 보여 줍니다.
// 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
데 사용할 수 있습니다.
속성에는 TryStatements 블록 내에서 실행할 문이 포함되어 있습니다 try
. 속성에는 CatchClauses catch된 catch
예외를 처리하는 절이 포함되어 있습니다. 속성에는 FinallyStatements 블록 내에서 실행할 문이 포함되어 있습니다 finally
.
참고
모든 언어가 블록을 지원하는 try
/catch
것은 아닙니다. 플래그를 Supports 사용하여 메서드를 GeneratorSupport.TryCatchStatements 호출하여 코드 생성기가 블록을 지원하는지 여부를 확인합니다/try
catch
.
생성자
CodeTryCatchFinallyStatement() |
CodeTryCatchFinallyStatement 클래스의 새 인스턴스를 초기화합니다. |
CodeTryCatchFinallyStatement(CodeStatement[], CodeCatchClause[]) |
try 및 catch 절에 대해 지정한 문을 사용하여 CodeTryCatchFinallyStatement 클래스의 새 인스턴스를 초기화합니다. |
CodeTryCatchFinallyStatement(CodeStatement[], CodeCatchClause[], CodeStatement[]) |
try 절, catch 절 및 finally 문에 대해 지정한 문을 사용하여 CodeTryCatchFinallyStatement 클래스의 새 인스턴스를 초기화합니다. |
속성
CatchClauses |
사용할 catch 절을 가져옵니다. |
EndDirectives |
끝 지시문이 포함된 CodeDirectiveCollection 개체를 가져옵니다. (다음에서 상속됨 CodeStatement) |
FinallyStatements |
사용할 finally 문을 가져옵니다. |
LinePragma |
코드 문이 있는 줄을 가져오거나 설정합니다. (다음에서 상속됨 CodeStatement) |
StartDirectives |
시작 지시문이 포함된 CodeDirectiveCollection 개체를 가져옵니다. (다음에서 상속됨 CodeStatement) |
TryStatements |
try할 문을 가져옵니다. |
UserData |
현재 개체에 대해 사용자 정의 가능한 데이터를 가져옵니다. (다음에서 상속됨 CodeObject) |
메서드
Equals(Object) |
지정된 개체가 현재 개체와 같은지 확인합니다. (다음에서 상속됨 Object) |
GetHashCode() |
기본 해시 함수로 작동합니다. (다음에서 상속됨 Object) |
GetType() |
현재 인스턴스의 Type을 가져옵니다. (다음에서 상속됨 Object) |
MemberwiseClone() |
현재 Object의 단순 복사본을 만듭니다. (다음에서 상속됨 Object) |
ToString() |
현재 개체를 나타내는 문자열을 반환합니다. (다음에서 상속됨 Object) |
적용 대상
.NET