Patterns.TryWith Active Pattern (F#)

Recognizes expressions that represent a try...with construct for exception filtering and catching.

Namespace/Module Path: Microsoft.FSharp.Quotations.Patterns

Assembly: FSharp.Core (in FSharp.Core.dll)

// Signature:
( |TryWith|_| ) : (input:Expr) -> (Expr * Var * Expr * Var * Expr) option

Parameters

  • input
    Type: Expr

    The input expression to match against.

Return Value

The formal return type is (Expr * Var * Expr * Var * Expr) option. The option indicates whether a successful match is made. In a pattern matching expression, upon a successful match, the input expression is decomposed into a tuple of five elements. The first element is an expression that represents the body of the try...with expression. The second element is the filter value, which is the value that is used to compare against the patterns. The third element is an expression that represents the filtering and assignment of any values set in the pattern matching, for example, by using the as keyword. The fourth element is the catch value, which is usually the same as the filter value and is used to determine which branch is taken. The final element is the catch expression, which includes the branching code. The tuple elements correspond to the arguments of the Expr.TryWith method.

Remarks

This function is named TryWithPattern in the .NET Framework assembly. If you are accessing the member from a .NET Framework language other than F#, or through reflection, use this name.

Example

The following example shows how a simple try ... with expression is decomposed.

open Microsoft.FSharp.Quotations.Patterns
module TryWithPattern =
    let f x =
       if (x = 0) then
           raise (new System.DivideByZeroException())
       else
           100 / x

    let expr =
       <@
        try
           f 20
        with
        | :? System.DivideByZeroException as exn ->
            -1
        | exn -> -2 @>

    match expr with
    | TryWith(tryExpr, filterValue, filterExpr, catchValue, catchExpr) ->
        printfn "Try Expression: %s\n" (tryExpr.ToString())
        printfn "Filter value: %s\n" (filterValue.Name)
        printfn "Filter expression: %s\n" (filterExpr.ToString())
        printfn "Catch value: %s\n" (catchValue.Name)
        printfn "Catch expression: %s\n" (catchExpr.ToString())
    | _ -> printfn "Unrecognized expression."

The output shows how the expressions are decomposed.

Try Expression: Call (None, Int32 f(Int32), [Value (20)])

Filter value: matchValue

Filter expression: IfThenElse (TypeTest (System.DivideByZeroException, matchValue),
            Let (exn,
                 Call (None,
                       System.DivideByZeroException UnboxGeneric[DivideByZeroException](System.Object),
                       [matchValue]), Value (1)),
            Let (exn, matchValue, Value (1)))

Catch value: matchValue

Catch expression: IfThenElse (TypeTest (System.DivideByZeroException, matchValue),
            Let (exn,
                 Call (None,
                       System.DivideByZeroException UnboxGeneric[DivideByZeroException](System.Object),
                       [matchValue]), Value (-1)),
            Let (exn, matchValue, Value (-2)))

Platforms

Windows 7, Windows Vista SP2, Windows XP SP3, Windows XP x64 SP2, Windows Server 2008 R2, Windows Server 2008 SP2, Windows Server 2003 SP2

Version Information

F# Runtime

Supported in: 2.0, 4.0

Silverlight

Supported in: 3

See Also

Reference

Quotations.Patterns Module (F#)

Microsoft.FSharp.Quotations Namespace (F#)