CodeTryCatchFinallyStatement Klas
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Vertegenwoordigt een try blok met een willekeurig aantal catch componenten en, optioneel, een finally blok.
public ref 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
[<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
- Overname
- Kenmerken
Voorbeelden
In de volgende voorbeeldcode ziet u hoe u een CodeTryCatchFinallyStatement instructie definieert try...catch...finally voor het verwerken van uitzonderingen.
// 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
Opmerkingen
CodeTryCatchFinallyStatement kan worden gebruikt om een try/catch codeblok weer te geven.
De TryStatements eigenschap bevat de instructies die binnen een try blok moeten worden uitgevoerd. De CatchClauses eigenschap bevat de componenten voor het catch afhandelen van gevangen uitzonderingen. De FinallyStatements eigenschap bevat de instructies die binnen een finally blok moeten worden uitgevoerd.
Opmerking
Niet alle talen ondersteunen try/catch blokken. Roep de Supports methode aan met de GeneratorSupport.TryCatchStatements vlag om te bepalen of een codegenerator blokken ondersteunt/trycatch.
Constructors
| Name | Description |
|---|---|
| CodeTryCatchFinallyStatement() |
Initialiseert een nieuw exemplaar van de CodeTryCatchFinallyStatement klasse. |
| CodeTryCatchFinallyStatement(CodeStatement[], CodeCatchClause[], CodeStatement[]) |
Initialiseert een nieuw exemplaar van de CodeTryCatchFinallyStatement klasse met behulp van de opgegeven instructies voor try, catch-componenten en tot slot instructies. |
| CodeTryCatchFinallyStatement(CodeStatement[], CodeCatchClause[]) |
Initialiseert een nieuw exemplaar van de CodeTryCatchFinallyStatement klasse met behulp van de opgegeven instructies voor try and catch-componenten. |
Eigenschappen
| Name | Description |
|---|---|
| CatchClauses |
Hiermee haalt u de catch-componenten op die moeten worden gebruikt. |
| EndDirectives |
Hiermee haalt u een CodeDirectiveCollection object op dat eindrichtlijnen bevat. (Overgenomen van CodeStatement) |
| FinallyStatements |
Hiermee haalt u de definitieve instructies op die moeten worden gebruikt. |
| LinePragma |
Hiermee haalt u de regel op waarop de code-instructie plaatsvindt of stelt u deze in. (Overgenomen van CodeStatement) |
| StartDirectives |
Hiermee haalt u een CodeDirectiveCollection object op dat beginrichtlijnen bevat. (Overgenomen van CodeStatement) |
| TryStatements |
Haalt de instructies op die moeten worden geprobeerd. |
| UserData |
Haalt de door de gebruiker gedefinieerde gegevens voor het huidige object op. (Overgenomen van CodeObject) |
Methoden
| Name | Description |
|---|---|
| Equals(Object) |
Bepaalt of het opgegeven object gelijk is aan het huidige object. (Overgenomen van Object) |
| GetHashCode() |
Fungeert als de standaardhashfunctie. (Overgenomen van Object) |
| GetType() |
Hiermee haalt u de Type huidige instantie op. (Overgenomen van Object) |
| MemberwiseClone() |
Hiermee maakt u een ondiepe kopie van de huidige Object. (Overgenomen van Object) |
| ToString() |
Retourneert een tekenreeks die het huidige object vertegenwoordigt. (Overgenomen van Object) |