System.Linq.Expressions.Expression.Catch C# (CSharp) Method

Catch() public static method

Creates a CatchBlock representing a catch statement with an Exception filter and a reference to the caught Exception object.
public static Catch ( System.Linq.Expressions.ParameterExpression variable, Expression body, Expression filter ) : CatchBlock
variable System.Linq.Expressions.ParameterExpression A representing a reference to the object caught by this handler.
body Expression The body of the catch statement.
filter Expression The body of the filter.
return CatchBlock
        public static CatchBlock Catch(ParameterExpression variable, Expression body, Expression filter)
        {
            ContractUtils.RequiresNotNull(variable, nameof(variable));
            return MakeCatchBlock(variable.Type, variable, body, filter);
        }

Same methods

Expression::Catch ( System.Linq.Expressions.ParameterExpression variable, Expression body ) : CatchBlock
Expression::Catch ( Type type, Expression body ) : CatchBlock
Expression::Catch ( Type type, Expression body, Expression filter ) : CatchBlock

Usage Example

コード例 #1
0
        public EvaluationCallback Create(Node node)
        {
            var compilerstate = new CompilerState
            {
                FunctionState = Exp.Parameter(typeof(Character), "state"),
                ErrorVariable = Exp.Parameter(typeof(bool), "error")
            };
            var result = Make(compilerstate, node);

            if (result.Type == typeof(bool))
            {
                result = ToInteger(result);
            }
            if (result.Type == typeof(int) || result.Type == typeof(float))
            {
                // int or float convert to number
                var constructor = typeof(Number).GetConstructor(new[] { result.Type });
                result = Exp.New(constructor, new[] { result });

                // wrap the evaluation in a try..catch
                var exceptionParameter = Exp.Parameter(typeof(Exception), "e");
                var writeLineMethod    = typeof(Console).GetMethod(nameof(Console.WriteLine), new Type[] { typeof(string) });
                var toStringMethod     = typeof(Exception).GetMethod(nameof(Exception.ToString));
                var catchBody          = Exp.Block(
                    Exp.Call(null, writeLineMethod, Exp.Call(exceptionParameter, toStringMethod)),
                    Exp.Constant(new Number(0)));
                result = Exp.TryCatch(result, Exp.Catch(exceptionParameter, catchBody));
                // create lambda
                var func = Exp.Lambda <Func <Character, bool, Number> >(result, compilerstate.FunctionState, compilerstate.ErrorVariable).Compile();
                return(new EvaluationCallback(o => func(o, false)));
            }
            throw new Exception();
        }
All Usage Examples Of System.Linq.Expressions.Expression::Catch
Expression