I'm struggling to build an expression that if the condition is true throws an exception and if it's false that it should return a value but I'm always getting the ArgumentException
:
var expr =
Expression.Condition(
Expression.Equal(Expression.Constant(0), Expression.Constant(0)),
Expression.Throw(Expression.Constant(new DivideByZeroException())),
Expression.Constant(1));
var lambda = Expression.Lambda<Func<int>>(expr);
var result = lambda.Compile()();
If I put Expression.Empty()
as the third argument of the Condition
it then runs but I don't get the desired result if the condition is false.