Auf Englisch lesen

Teilen über


CodeTryCatchFinallyStatement Klasse

Definition

Stellt einen try-Block mit einer beliebigen Anzahl von catch-Klauseln und einem optionalen finally-Block dar.

C#
public class CodeTryCatchFinallyStatement : System.CodeDom.CodeStatement
C#
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public class CodeTryCatchFinallyStatement : System.CodeDom.CodeStatement
Vererbung
CodeTryCatchFinallyStatement
Attribute

Beispiele

Der folgende Beispielcode veranschaulicht die Verwendung einer CodeTryCatchFinallyStatement , um eine Anweisung für die try...catch...finally Ausnahmebehandlung zu definieren.

C#
// 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");
//        }
//    }

Hinweise

CodeTryCatchFinallyStatement kann verwendet werden, um einen try/catch Codeblock darzustellen.

Die TryStatements -Eigenschaft enthält die Anweisungen, die in einem try Block ausgeführt werden sollen. Die CatchClauses -Eigenschaft enthält die catch Klauseln, die abgefangene Ausnahmen behandeln sollen. Die FinallyStatements -Eigenschaft enthält die Anweisungen, die in einem finally Block ausgeführt werden sollen.

Hinweis

Nicht alle Sprachen unterstützen try/catch Blöcke. Rufen Sie die Supports -Methode mit dem GeneratorSupport.TryCatchStatements Flag auf, um zu bestimmen, ob ein Codegenerator Blöcke unterstützt/trycatch.

Konstruktoren

CodeTryCatchFinallyStatement()

Initialisiert eine neue Instanz der CodeTryCatchFinallyStatement-Klasse.

CodeTryCatchFinallyStatement(CodeStatement[], CodeCatchClause[])

Initialisiert eine neue Instanz der CodeTryCatchFinallyStatement-Klasse unter Verwendung der angegebenen try-Anweisungen und catch-Abschnitte.

CodeTryCatchFinallyStatement(CodeStatement[], CodeCatchClause[], CodeStatement[])

Initialisiert eine neue Instanz der CodeTryCatchFinallyStatement-Klasse unter Verwendung der angegebenen try-Anweisungen, catch-Abschnitte und finally-Anweisungen.

Eigenschaften

CatchClauses

Ruft die zu verwendenden catch-Klauseln ab.

EndDirectives

Ruft ein CodeDirectiveCollection-Objekt ab, das Enddirektiven enthält.

(Geerbt von CodeStatement)
FinallyStatements

Ruft die zu verwendenden finally-Anweisungen ab.

LinePragma

Ruft die Zeile ab, in der die Codeanweisung auftritt, oder legt diese fest.

(Geerbt von CodeStatement)
StartDirectives

Ruft ein CodeDirectiveCollection-Objekt ab, das Startdirektiven enthält.

(Geerbt von CodeStatement)
TryStatements

Ruft die try-Anweisungen ab.

UserData

Ruft die benutzerdefinierbaren Daten für das aktuelle Objekt ab.

(Geerbt von CodeObject)

Methoden

Equals(Object)

Bestimmt, ob das angegebene Objekt gleich dem aktuellen Objekt ist.

(Geerbt von Object)
GetHashCode()

Fungiert als Standardhashfunktion.

(Geerbt von Object)
GetType()

Ruft den Type der aktuellen Instanz ab.

(Geerbt von Object)
MemberwiseClone()

Erstellt eine flache Kopie des aktuellen Object.

(Geerbt von Object)
ToString()

Gibt eine Zeichenfolge zurück, die das aktuelle Objekt darstellt.

(Geerbt von Object)

Gilt für:

Produkt Versionen
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9