Expression.TryCatch(Expression, CatchBlock[]) Yöntem

Tanım

Herhangi bir TryExpression sayıda catch deyimi ve ne hata ne de son olarak blok içeren bir try bloğunu temsil eden bir oluşturur.

C#
public static System.Linq.Expressions.TryExpression TryCatch (System.Linq.Expressions.Expression body, params System.Linq.Expressions.CatchBlock[] handlers);
C#
public static System.Linq.Expressions.TryExpression TryCatch (System.Linq.Expressions.Expression body, params System.Linq.Expressions.CatchBlock[]? handlers);

Parametreler

body
Expression

Try bloğunun gövdesi.

handlers
CatchBlock[]

Try bloğuyla ilişkilendirilecek catch deyimlerini temsil eden sıfır veya daha fazla CatchBlock ifade dizisi.

Döndürülenler

Oluşturulan TryExpression.

Örnekler

Aşağıdaki örnekte catch deyimi içeren bir TryExpression nesnenin nasıl oluşturulacağı gösterilmektedir.

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

Şunlara uygulanır

Ürün Sürümler
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0