TryExpression 類別

定義

代表 try/catch/finally/fault 區塊。Represents a try/catch/finally/fault block.

public ref class TryExpression sealed : System::Linq::Expressions::Expression
public sealed class TryExpression : System.Linq.Expressions.Expression
type TryExpression = class
    inherit Expression
Public NotInheritable Class TryExpression
Inherits Expression
繼承
TryExpression

範例

下列範例示範如何 TryExpression 使用方法建立包含 catch 語句的物件 TryCatchThe following example demonstrates how to create a TryExpression object that contains a catch statement by using the TryCatch method.

// Add the following directive to the file:
// using System.Linq.Expressions;

// A TryExpression object that has a Catch statement.
// The return types of the Try block and all Catch blocks must be the same.
TryExpression tryCatchExpr =
    Expression.TryCatch(
        Expression.Block(
            Expression.Throw(Expression.Constant(new DivideByZeroException())),
            Expression.Constant("Try block")
        ),
        Expression.Catch(
            typeof(DivideByZeroException),
            Expression.Constant("Catch block")
        )
    );

// The following statement first creates an expression tree,
// then compiles it, and then runs it.
// If the exception is caught,
// the result of the TryExpression is the last statement
// of the corresponding Catch statement.
Console.WriteLine(Expression.Lambda<Func<string>>(tryCatchExpr).Compile()());

// This code example produces the following output:
//
// Catch block
' Add the following directive to the file:
' Imports System.Linq.Expressions 

' A TryExpression object that has a Catch statement.
' The return types of the Try block and all Catch blocks must be the same.
Dim tryCatchExpr As TryExpression =
       Expression.TryCatch(
           Expression.Block(
               Expression.Throw(Expression.Constant(New DivideByZeroException())),
               Expression.Constant("Try block")
           ),
           Expression.Catch(
               GetType(DivideByZeroException),
               Expression.Constant("Catch block")
           )
       )

' The following statement first creates an expression tree,
' then compiles it, and then runs it.
' If the exception is caught, 
' the result of the TryExpression is the last statement 
' of the corresponding Catch statement.
Console.WriteLine(Expression.Lambda(Of Func(Of String))(tryCatchExpr).Compile()())

' This code example produces the following output:
'
' Catch block

備註

主體區塊是由 try 區塊所保護。The body block is protected by the try block.

處理常式是由一組 CatchBlock 可以是 catch 語句或篩選準則的運算式所組成。The handlers consist of a set of CatchBlock expressions that can be either catch statements or filters.

如果擲回例外狀況,則會執行錯誤區塊。The fault block runs if an exception is thrown.

Finally 區塊會執行,不論控制項離開主體的方式為何。The finally block runs regardless of how control exits the body.

只能提供一個錯誤或 finally 區塊。Only one of fault or finally blocks can be supplied.

Try 區塊的傳回型別必須符合任何相關 catch 語句的傳回型別。The return type of the try block must match the return type of any associated catch statements.

屬性

Body

取得表示 try 區塊主體的 ExpressionGets the Expression representing the body of the try block.

CanReduce

表示節點可精簡為更簡單的節點。Indicates that the node can be reduced to a simpler node. 如果傳回 true,則可呼叫 Reduce() 以產生精簡的形式。If this returns true, Reduce() can be called to produce the reduced form.

(繼承來源 Expression)
Fault

取得表示 fault 區塊的 ExpressionGets the Expression representing the fault block.

Finally

取得表示 finally 區塊的 ExpressionGets the Expression representing the finally block.

Handlers

取得與 try 區塊相關聯的 CatchBlock 運算式集合。Gets the collection of CatchBlock expressions associated with the try block.

NodeType

傳回這個 Expression 的節點類型。Returns the node type of this Expression.

Type

取得此 Expression 代表之運算式的靜態類型。Gets the static type of the expression that this Expression represents.

方法

Accept(ExpressionVisitor)

分派給這個節點類型的特定造訪方法。Dispatches to the specific visit method for this node type. 例如,MethodCallExpression 會呼叫 VisitMethodCall(MethodCallExpression)For example, MethodCallExpression calls the VisitMethodCall(MethodCallExpression).

(繼承來源 Expression)
Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
Reduce()

將這個節點精簡為更簡單的運算式。Reduces this node to a simpler expression. 如果 CanReduce 傳回 true,則應該傳回有效的運算式。If CanReduce returns true, this should return a valid expression. 這個方法可以傳回其他本身必須精簡的節點。This method can return another node which itself must be reduced.

(繼承來源 Expression)
ReduceAndCheck()

將這個節點精簡為更簡單的運算式。Reduces this node to a simpler expression. 如果 CanReduce 傳回 true,則應該傳回有效的運算式。If CanReduce returns true, this should return a valid expression. 這個方法可以傳回其他本身必須精簡的節點。This method can return another node which itself must be reduced.

(繼承來源 Expression)
ReduceExtensions()

將運算式精簡為已知的節點類型 (不是 Extension 節點),如果已經是已知的類型,則僅傳回運算式。Reduces the expression to a known node type (that is not an Extension node) or just returns the expression if it is already a known type.

(繼承來源 Expression)
ToString()

傳回 Expression 的文字表示。Returns a textual representation of the Expression.

(繼承來源 Expression)
Update(Expression, IEnumerable<CatchBlock>, Expression, Expression)

建立類似這個運算式的新運算式,但使用提供的子系。Creates a new expression that is like this one, but using the supplied children. 如果所有子系都相同,則會傳回這個運算式。If all of the children are the same, it will return this expression.

VisitChildren(ExpressionVisitor)

精簡節點,然後呼叫精簡後的運算式上的訪問項委派。Reduces the node and then calls the visitor delegate on the reduced expression. 如果節點無法縮減,此方法會擲回例外狀況。The method throws an exception if the node is not reducible.

(繼承來源 Expression)

適用於