CodeTryCatchFinallyStatement-Klasse
Stellt einen try-Block mit einer beliebigen Anzahl von catch-Klauseln und einem optionalen finally-Block dar.
Namespace: System.CodeDom
Assembly: System (in system.dll)
Syntax
'Declaration
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
<ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _
Public Class CodeTryCatchFinallyStatement
Inherits CodeStatement
'Usage
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
Hinweise
Mit CodeTryCatchFinallyStatement kann ein try/catch-Codeblock dargestellt werden.
Die TryStatements-Eigenschaft enthält die Anweisungen, die innerhalb eines try-Blocks ausgeführt werden sollen. Die CatchClauses-Eigenschaft enthält die catch-Klauseln zur Behandlung von abgefangenen Ausnahmen. Die FinallyStatements-Eigenschaft enthält die Anweisungen, die innerhalb eines finally-Blocks ausgeführt werden sollen.
Hinweis
try/catch-Blöcke werden nicht von allen Sprachen unterstützt. Rufen Sie die Supports-Methode mit dem GeneratorSupport.TryCatchStatements-Flag auf, um zu bestimmen, ob ein Code-Generator try/catch-Blöcke unterstützt.
Beispiel
Im folgenden Beispielcode wird veranschaulicht, wie mit einem CodeTryCatchFinallyStatement eine try...catch...finally-Anweisung für die Ausnahmebehandlung definiert wird.
' 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");
// }
// }
Vererbungshierarchie
System.Object
System.CodeDom.CodeObject
System.CodeDom.CodeStatement
System.CodeDom.CodeTryCatchFinallyStatement
Threadsicherheit
Alle öffentlichen statischen (Shared in Visual Basic) Member dieses Typs sind threadsicher. Bei Instanzmembern ist die Threadsicherheit nicht gewährleistet.
Plattformen
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.
Versionsinformationen
.NET Framework
Unterstützt in: 2.0, 1.1, 1.0
Siehe auch
Referenz
CodeTryCatchFinallyStatement-Member
System.CodeDom-Namespace