Expression.Throw 方法

定義

建立 UnaryExpression,表示擲回例外狀況。Creates a UnaryExpression that represents a throwing of an exception.

多載

Throw(Expression)

建立 UnaryExpression,表示擲回例外狀況。Creates a UnaryExpression that represents a throwing of an exception.

Throw(Expression, Type)

建立 UnaryExpression,代表擲回含指定之類型的例外狀況。Creates a UnaryExpression that represents a throwing of an exception with a given type.

Throw(Expression)

建立 UnaryExpression,表示擲回例外狀況。Creates a UnaryExpression that represents a throwing of an exception.

public:
 static System::Linq::Expressions::UnaryExpression ^ Throw(System::Linq::Expressions::Expression ^ value);
public static System.Linq.Expressions.UnaryExpression Throw (System.Linq.Expressions.Expression value);
public static System.Linq.Expressions.UnaryExpression Throw (System.Linq.Expressions.Expression? value);
static member Throw : System.Linq.Expressions.Expression -> System.Linq.Expressions.UnaryExpression
Public Shared Function Throw (value As Expression) As UnaryExpression

參數

傳回

UnaryExpression

表示例外狀況的 UnaryExpressionA UnaryExpression that represents the exception.

範例

下列範例示範如何建立 TryExpression 使用方法的物件 ThrowThe following example demonstrates how to create a TryExpression object that uses the Throw 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

適用於

Throw(Expression, Type)

建立 UnaryExpression,代表擲回含指定之類型的例外狀況。Creates a UnaryExpression that represents a throwing of an exception with a given type.

public:
 static System::Linq::Expressions::UnaryExpression ^ Throw(System::Linq::Expressions::Expression ^ value, Type ^ type);
public static System.Linq.Expressions.UnaryExpression Throw (System.Linq.Expressions.Expression value, Type type);
public static System.Linq.Expressions.UnaryExpression Throw (System.Linq.Expressions.Expression? value, Type type);
static member Throw : System.Linq.Expressions.Expression * Type -> System.Linq.Expressions.UnaryExpression
Public Shared Function Throw (value As Expression, type As Type) As UnaryExpression

參數

type
Type

運算式的新 TypeThe new Type of the expression.

傳回

UnaryExpression

表示例外狀況的 UnaryExpressionA UnaryExpression that represents the exception.

適用於