CodeTryCatchFinallyStatement Třída

Definice

try Představuje blok s libovolným počtem catch klauzulí a volitelně blokemfinally.

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
Dědičnost
CodeTryCatchFinallyStatement
Atributy

Příklady

Následující příklad kódu ukazuje použití CodeTryCatchFinallyStatement k definování try...catch...finally příkazu pro zpracování výjimek.

// 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

Poznámky

CodeTryCatchFinallyStatement lze použít k reprezentaci try/catch bloku kódu.

Vlastnost TryStatements obsahuje příkazy, které se mají provést v rámci try bloku. Vlastnost CatchClauses obsahuje klauzule catch pro zpracování zachycených výjimek. Vlastnost FinallyStatements obsahuje příkazy, které se mají provést v rámci finally bloku.

Poznámka

Ne všechny jazyky bloky podporují try/catch . Supports Volání metody s příznakem GeneratorSupport.TryCatchStatements k určení, zda generátor kódu podporuje try/catch bloky.

Konstruktory

CodeTryCatchFinallyStatement()

Inicializuje novou instanci CodeTryCatchFinallyStatement třídy .

CodeTryCatchFinallyStatement(CodeStatement[], CodeCatchClause[])

Inicializuje novou instanci CodeTryCatchFinallyStatement třídy pomocí zadaných příkazů pro klauzule try a catch.

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

Inicializuje novou instanci CodeTryCatchFinallyStatement třídy pomocí zadaných příkazů pro klauzule try, catch a finally příkazy.

Vlastnosti

CatchClauses

Získá klauzule catch k použití.

EndDirectives

CodeDirectiveCollection Získá objekt, který obsahuje koncové direktivy.

(Zděděno od CodeStatement)
FinallyStatements

Získá příkazy finally k použití.

LinePragma

Získá nebo nastaví řádek, na kterém dochází k příkazu kódu.

(Zděděno od CodeStatement)
StartDirectives

CodeDirectiveCollection Získá objekt, který obsahuje start direktivy.

(Zděděno od CodeStatement)
TryStatements

Získá příkazy, které chcete vyzkoušet.

UserData

Získá uživatelsky definovatelná data pro aktuální objekt.

(Zděděno od CodeObject)

Metody

Equals(Object)

Určí, zda se zadaný objekt rovná aktuálnímu objektu.

(Zděděno od Object)
GetHashCode()

Slouží jako výchozí hashovací funkce.

(Zděděno od Object)
GetType()

Type Získá z aktuální instance.

(Zděděno od Object)
MemberwiseClone()

Vytvoří mělkou kopii aktuálního Objectsouboru .

(Zděděno od Object)
ToString()

Vrátí řetězec, který představuje aktuální objekt.

(Zděděno od Object)

Platí pro