/// <summary> /// Adds <see langword="catch" /> statement catching <see cref="Exception"/> specified as <typeparamref name="T"/> /// </summary> /// <param name="catch"><see langword="catch" /> block body</param> /// <typeparam name="T"><see cref="Exception"/> type to catch</typeparam> /// <returns></returns> public TryCatchFinallyBuilder Catch <T>(Func <ExpressionContainer <T>, Expression> @catch) where T : Exception { var exception = ExpressionShortcuts.Var <T>(); var body = @catch(exception); var catchBlock = Expression.Catch((ParameterExpression)exception, body); return(Catch(catchBlock)); }
/// <summary> /// Adds <see langword="catch" /> statement catching all <see cref="Exception"/> types /// </summary> /// <param name="catch"></param> /// <param name="when"></param> /// <typeparam name="T"></typeparam> /// <returns></returns> public TryCatchFinallyBuilder Catch <T>(Func <ExpressionContainer <T>, Expression> @catch, Func <ExpressionContainer <T>, ExpressionContainer <bool> > when) where T : Exception { var exception = ExpressionShortcuts.Var <T>(); var body = @catch(exception); var filter = when?.Invoke(exception); var catchBlock = filter == null ? Expression.Catch((ParameterExpression)exception, body) : Expression.Catch((ParameterExpression)exception, body, filter); return(Catch(catchBlock)); }