Example #1
0
        /// <summary>
        /// Invokes the action <paramref name="action"/> and handles the exception
        /// <typeparamref name="TException"/>.  Action <paramref name="exceptionHanldedAction"/>
        /// will be invoked if the exception is handled.
        /// </summary>
        /// <typeparam name="TException"><see cref="Exception"/></typeparam>
        /// <typeparam name="TResult">type of result</typeparam>
        /// <param name="action">action</param>
        /// <param name="exceptionHanldedAction">action</param>
        /// <returns></returns>
        public static TResult Inline <TException, TResult>(Func <TResult> action, Action <Exception> exceptionHanldedAction = null) where TException : Exception
        {
            HandleExceptionHolder holder = new HandleExceptionHolder((exception) => exception is TException ? exception : null);

            try
            {
                return(action.Invoke());
            }
            catch (Exception ex)
            {
                if (holder.FirstOrDefault(ex) == null)
                {
                    throw;
                }
                else
                {
                    if (exceptionHanldedAction != null)
                    {
                        exceptionHanldedAction.Invoke(ex);
                    }
                }
            }

            return(default(TResult));
        }
Example #2
0
        /// <summary>
        /// Invokes the action <paramref name="action"/> and handles the exception
        /// <typeparamref name="TException"/>.  Action <paramref name="exceptionHanldedAction"/>
        /// will be invoked if the exception is handled.
        /// </summary>
        /// <typeparam name="TException"><see cref="Exception"/></typeparam>
        /// <typeparam name="TInput">type of input</typeparam>
        /// <param name="action">action</param>
        /// <param name="input"><typeparamref name="TInput"/></param>
        /// <param name="exceptionHanldedAction">action</param>
        public static void Inline <TException, TInput>(Action <TInput> action, TInput input, Action <Exception> exceptionHanldedAction = null) where TException : Exception
        {
            HandleExceptionHolder holder = new HandleExceptionHolder((exception) => exception is TException ? exception : null);

            try
            {
                action.Invoke(input);
            }
            catch (Exception ex)
            {
                if (holder.FirstOrDefault(ex) == null)
                {
                    throw;
                }
                else
                {
                    if (exceptionHanldedAction != null)
                    {
                        exceptionHanldedAction.Invoke(ex);
                    }
                }
            }
        }