TryExpression 类 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示一个 try/catch/finally/fault 块。
public ref class TryExpression sealed : System::Linq::Expressions::Expressionpublic sealed class TryExpression : System.Linq.Expressions.Expressiontype TryExpression = class
    inherit ExpressionPublic NotInheritable Class TryExpression
Inherits Expression- 继承
示例
下面的示例演示如何使用 TryCatch 方法创建TryExpression包含 catch 语句的 对象。
// 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 块保护。
处理程序由一组 CatchBlock 表达式组成,这些表达式可以是 catch 语句或筛选器。
如果引发异常,故障块将运行。
不管控件如何退出正文,finally 块都会运行。
只能提供故障块或最终块之一。
try 块的返回类型必须与任何关联的 catch 语句的返回类型匹配。
属性
| Body | 获取表示 try 块的主体的 Expression。 | 
| CanReduce | 指示可将节点简化为更简单的节点。 如果返回 true,则可以调用 Reduce() 以生成简化形式。(继承自 Expression) | 
| Fault | 获取表示 fault 块的 Expression。 | 
| Finally | 获取表示 finally 块的 Expression。 | 
| Handlers | 获取与 try 块关联的 CatchBlock 表达式的集合。 | 
| NodeType | 返回此 Expression 的节点类型。 | 
| Type | 获取此 Expression 表示的表达式的静态类型。 | 
方法
| Accept(ExpressionVisitor) | 调度到此节点类型的特定 Visit 方法。 例如,MethodCallExpression 调用 VisitMethodCall(MethodCallExpression)。(继承自 Expression) | 
| Equals(Object) | 确定指定对象是否等于当前对象。(继承自 Object) | 
| GetHashCode() | 作为默认哈希函数。(继承自 Object) | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| Reduce() | 将此节点简化为更简单的表达式。 如果 CanReduce 返回 true,则它应返回有效的表达式。 此方法可以返回本身必须简化的另一个节点。(继承自 Expression) | 
| ReduceAndCheck() | 将此节点简化为更简单的表达式。 如果 CanReduce 返回 true,则它应返回有效的表达式。 此方法可以返回本身必须简化的另一个节点。(继承自 Expression) | 
| ReduceExtensions() | 将表达式简化为已知节点类型(即非 Extension 节点)或仅在此类型为已知类型时返回表达式。(继承自 Expression) | 
| ToString() | 返回 Expression 的的文本化表示形式。(继承自 Expression) | 
| Update(Expression, IEnumerable<CatchBlock>, Expression, Expression) | 创建一个新的表达式,它类似于此表达式,但使用所提供的子级。 如果所有子级均相同,它将返回此表达式。 | 
| VisitChildren(ExpressionVisitor) | 简化节点,然后对简化的表达式调用访问者委托。 该方法在节点不可简化时引发异常。(继承自 Expression) |