Skip to main content
Replaced all html entities in code snippet with actual characters
Source Link
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common.Helper
{
    public static class FunctionalHelpers
    {
        public static void TryCatch&lt;TE&gt;TryCatch<TE>(Action tryAction, Action&lt;Exception&gt;Action<Exception> handler)
          where TE : Exception
        {
            try { tryAction(); }
            catch (TE ex)
            {
                handler(ex);
            }
        }

        public static void TryCatch&lt;TArgTryCatch<TArg, TE&gt;TE>(Action&lt;TArg&gt;Action<TArg> tryAction, TArg args, Action&lt;Exception&gt;Action<Exception> handler)
          where TE : Exception
        {
            try { tryAction(args); }
            catch (TE ex)
            {
                handler(ex);
            }
        }

        public static void TryCatchFinally&lt;TE&gt;TryCatchFinally<TE>(Action tryAction, Action&lt;Exception&gt;Action<Exception> CatchAction, Action FinallyAction)
            where TE : Exception
        {
            try
            {
                tryAction();
            }
            catch (TE t)
            {
                CatchAction(t);
            }
            finally
            {
                FinallyAction();
            }
        }

        public static void TryCatchFinallyUsing&lt;TETryCatchFinallyUsing<TE, TUsing&gt;TUsing>(Action&lt;TUsing&gt;Action<TUsing> tryAction, Action&lt;Exception&gt;Action<Exception> CatchAction, Action&lt;TUsing&gt;Action<TUsing> FinallyAction, TUsing arg)
        where TE : Exception
        where TUsing : IDisposable
        {
            using (arg)
            {
                try
                {
                    tryAction(arg);
                }
                catch (TE t)
                {
                    CatchAction(t);
                }
                finally
                {
                    FinallyAction(arg);
                }
            }
        }

        public static void TryCatchFinallyUsing&lt;TE1TryCatchFinallyUsing<TE1, TE2, TUsing&gt;TUsing>(Action&lt;TUsing&gt;Action<TUsing> tryAction, Action&lt;TE1&gt;Action<TE1> CatchHandler1, Action&lt;TE2&gt;Action<TE2> CatchHandler2, Action&lt;TUsing&gt;Action<TUsing> FinallyAction, TUsing arg)
            where TE1 : Exception
            where TE2 : Exception
            where TUsing : IDisposable
        {
            using (arg)
            {
                try
                {
                    tryAction(arg);
                }
                catch (TE1 t)
                {
                    CatchHandler1(t);
                }
                catch (TE2 t)
                {
                    CatchHandler2(t);
                }
                finally
                {
                    FinallyAction(arg);
                }
            }
        }

        public static void TryCatchFinallyUsing&lt;TE1TryCatchFinallyUsing<TE1, TE2, TE3, TUsing&gt;TUsing>(Action&lt;TUsing&gt;Action<TUsing> tryAction, Action&lt;Exception&gt;Action<Exception> CatchHandler1, Action&lt;Exception&gt;Action<Exception> CatchHandler2, Action&lt;Exception&gt;Action<Exception> CatchHandler3, Action&lt;TUsing&gt;Action<TUsing> FinallyAction, TUsing arg)
            where TE1 : Exception
            where TE2 : Exception
            where TE3 : Exception
            where TUsing : IDisposable
        {
            using (arg)
            {
                try
                {
                    tryAction(arg);
                }
                catch (TE1 t)
                {
                    CatchHandler1(t);
                }
                catch (TE2 t)
                {
                    CatchHandler2(t);
                }
                catch (TE3 t)
                {
                    CatchHandler3(t);
                }
                finally
                {
                    FinallyAction(arg);
                }
            }
        }

        public static void TryCatchFinally&lt;TArgTryCatchFinally<TArg, TE&gt;TE>(Action&lt;TArg&gt;Action<TArg> tryAction, TArg arg, Action&lt;Exception&gt;Action<Exception> CatchAction, Action FinallyAction)
            where TE : Exception
        {
            try
            {
                tryAction(arg);
            }
            catch (TE t)
            {
                CatchAction(t);
            }
            finally
            {
                FinallyAction();
            }
        }

        public static void TryCatch&lt;T1&gt;TryCatch<T1>(Action tryAction, Action&lt;Exception&gt;Action<Exception> handler, Action FinallyAction)
            where T1 : Exception
        {
            try
            {
                tryAction();
            }
            catch (T1 ex)
            {
                handler(ex);
            }
            finally
            {
                FinallyAction();
            }
        }

        public static void TryCatchFinally&lt;T1TryCatchFinally<T1, TF&gt;TF>(Action tryAction, Action&lt;Exception&gt;Action<Exception> handler, Action&lt;TF&gt;Action<TF> finallyAction, TF FinallyArg)
            where T1 : Exception
        {
            try
            {
                tryAction();
            }
            catch (T1 ex)
            {
                handler(ex);
            }
            finally
            {
                finallyAction(FinallyArg);
            }
        }

        public static void TryCatchFinally&lt;TE1TryCatchFinally<TE1, TE2, TF&gt;TF>(Action tryAction, Action&lt;Exception&gt;Action<Exception> handler1, Action&lt;Exception&gt;Action<Exception> handler2, Action&lt;TF&gt;Action<TF> finallyAction, TF FinallyArg)
            where TE1 : Exception
            where TE2 : Exception
        {
            try
            {
                tryAction();
            }
            catch (TE1 ex)
            {
                handler1(ex);
            }
            catch (TE2 ex)
            {
                handler2(ex);
            }
            finally
            {
                finallyAction(FinallyArg);
            }
        }


        public static void TryCatchFinally&lt;TE1TryCatchFinally<TE1, TE2, TE3, TF&gt;TF>(Action tryAction, Action&lt;Exception&gt;Action<Exception> handler1, Action&lt;Exception&gt;Action<Exception> handler2, Action&lt;Exception&gt;Action<Exception> handler3, Action&lt;TF&gt;Action<TF> finallyAction, TF FinallyArg)
            where TE1 : Exception
            where TE2 : Exception
            where TE3 : Exception
        {
            try
            {
                tryAction();
            }
            catch (TE1 ex)
            {
                handler1(ex);
            }
            catch (TE2 ex)
            {
                handler2(ex);
            }
            catch (TE3 ex)
            {
                handler3(ex);
            }
            finally
            {
                finallyAction(FinallyArg);
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common.Helper
{
    public static class FunctionalHelpers
    {
        public static void TryCatch&lt;TE&gt;(Action tryAction, Action&lt;Exception&gt; handler)
          where TE : Exception
        {
            try { tryAction(); }
            catch (TE ex)
            {
                handler(ex);
            }
        }

        public static void TryCatch&lt;TArg, TE&gt;(Action&lt;TArg&gt; tryAction, TArg args, Action&lt;Exception&gt; handler)
          where TE : Exception
        {
            try { tryAction(args); }
            catch (TE ex)
            {
                handler(ex);
            }
        }

        public static void TryCatchFinally&lt;TE&gt;(Action tryAction, Action&lt;Exception&gt; CatchAction, Action FinallyAction)
            where TE : Exception
        {
            try
            {
                tryAction();
            }
            catch (TE t)
            {
                CatchAction(t);
            }
            finally
            {
                FinallyAction();
            }
        }

        public static void TryCatchFinallyUsing&lt;TE, TUsing&gt;(Action&lt;TUsing&gt; tryAction, Action&lt;Exception&gt; CatchAction, Action&lt;TUsing&gt; FinallyAction, TUsing arg)
        where TE : Exception
        where TUsing : IDisposable
        {
            using (arg)
            {
                try
                {
                    tryAction(arg);
                }
                catch (TE t)
                {
                    CatchAction(t);
                }
                finally
                {
                    FinallyAction(arg);
                }
            }
        }

        public static void TryCatchFinallyUsing&lt;TE1, TE2, TUsing&gt;(Action&lt;TUsing&gt; tryAction, Action&lt;TE1&gt; CatchHandler1, Action&lt;TE2&gt; CatchHandler2, Action&lt;TUsing&gt; FinallyAction, TUsing arg)
            where TE1 : Exception
            where TE2 : Exception
            where TUsing : IDisposable
        {
            using (arg)
            {
                try
                {
                    tryAction(arg);
                }
                catch (TE1 t)
                {
                    CatchHandler1(t);
                }
                catch (TE2 t)
                {
                    CatchHandler2(t);
                }
                finally
                {
                    FinallyAction(arg);
                }
            }
        }

        public static void TryCatchFinallyUsing&lt;TE1, TE2, TE3, TUsing&gt;(Action&lt;TUsing&gt; tryAction, Action&lt;Exception&gt; CatchHandler1, Action&lt;Exception&gt; CatchHandler2, Action&lt;Exception&gt; CatchHandler3, Action&lt;TUsing&gt; FinallyAction, TUsing arg)
            where TE1 : Exception
            where TE2 : Exception
            where TE3 : Exception
            where TUsing : IDisposable
        {
            using (arg)
            {
                try
                {
                    tryAction(arg);
                }
                catch (TE1 t)
                {
                    CatchHandler1(t);
                }
                catch (TE2 t)
                {
                    CatchHandler2(t);
                }
                catch (TE3 t)
                {
                    CatchHandler3(t);
                }
                finally
                {
                    FinallyAction(arg);
                }
            }
        }

        public static void TryCatchFinally&lt;TArg, TE&gt;(Action&lt;TArg&gt; tryAction, TArg arg, Action&lt;Exception&gt; CatchAction, Action FinallyAction)
            where TE : Exception
        {
            try
            {
                tryAction(arg);
            }
            catch (TE t)
            {
                CatchAction(t);
            }
            finally
            {
                FinallyAction();
            }
        }

        public static void TryCatch&lt;T1&gt;(Action tryAction, Action&lt;Exception&gt; handler, Action FinallyAction)
            where T1 : Exception
        {
            try
            {
                tryAction();
            }
            catch (T1 ex)
            {
                handler(ex);
            }
            finally
            {
                FinallyAction();
            }
        }

        public static void TryCatchFinally&lt;T1, TF&gt;(Action tryAction, Action&lt;Exception&gt; handler, Action&lt;TF&gt; finallyAction, TF FinallyArg)
            where T1 : Exception
        {
            try
            {
                tryAction();
            }
            catch (T1 ex)
            {
                handler(ex);
            }
            finally
            {
                finallyAction(FinallyArg);
            }
        }

        public static void TryCatchFinally&lt;TE1, TE2, TF&gt;(Action tryAction, Action&lt;Exception&gt; handler1, Action&lt;Exception&gt; handler2, Action&lt;TF&gt; finallyAction, TF FinallyArg)
            where TE1 : Exception
            where TE2 : Exception
        {
            try
            {
                tryAction();
            }
            catch (TE1 ex)
            {
                handler1(ex);
            }
            catch (TE2 ex)
            {
                handler2(ex);
            }
            finally
            {
                finallyAction(FinallyArg);
            }
        }


        public static void TryCatchFinally&lt;TE1, TE2, TE3, TF&gt;(Action tryAction, Action&lt;Exception&gt; handler1, Action&lt;Exception&gt; handler2, Action&lt;Exception&gt; handler3, Action&lt;TF&gt; finallyAction, TF FinallyArg)
            where TE1 : Exception
            where TE2 : Exception
            where TE3 : Exception
        {
            try
            {
                tryAction();
            }
            catch (TE1 ex)
            {
                handler1(ex);
            }
            catch (TE2 ex)
            {
                handler2(ex);
            }
            catch (TE3 ex)
            {
                handler3(ex);
            }
            finally
            {
                finallyAction(FinallyArg);
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common.Helper
{
    public static class FunctionalHelpers
    {
        public static void TryCatch<TE>(Action tryAction, Action<Exception> handler)
          where TE : Exception
        {
            try { tryAction(); }
            catch (TE ex)
            {
                handler(ex);
            }
        }

        public static void TryCatch<TArg, TE>(Action<TArg> tryAction, TArg args, Action<Exception> handler)
          where TE : Exception
        {
            try { tryAction(args); }
            catch (TE ex)
            {
                handler(ex);
            }
        }

        public static void TryCatchFinally<TE>(Action tryAction, Action<Exception> CatchAction, Action FinallyAction)
            where TE : Exception
        {
            try
            {
                tryAction();
            }
            catch (TE t)
            {
                CatchAction(t);
            }
            finally
            {
                FinallyAction();
            }
        }

        public static void TryCatchFinallyUsing<TE, TUsing>(Action<TUsing> tryAction, Action<Exception> CatchAction, Action<TUsing> FinallyAction, TUsing arg)
        where TE : Exception
        where TUsing : IDisposable
        {
            using (arg)
            {
                try
                {
                    tryAction(arg);
                }
                catch (TE t)
                {
                    CatchAction(t);
                }
                finally
                {
                    FinallyAction(arg);
                }
            }
        }

        public static void TryCatchFinallyUsing<TE1, TE2, TUsing>(Action<TUsing> tryAction, Action<TE1> CatchHandler1, Action<TE2> CatchHandler2, Action<TUsing> FinallyAction, TUsing arg)
            where TE1 : Exception
            where TE2 : Exception
            where TUsing : IDisposable
        {
            using (arg)
            {
                try
                {
                    tryAction(arg);
                }
                catch (TE1 t)
                {
                    CatchHandler1(t);
                }
                catch (TE2 t)
                {
                    CatchHandler2(t);
                }
                finally
                {
                    FinallyAction(arg);
                }
            }
        }

        public static void TryCatchFinallyUsing<TE1, TE2, TE3, TUsing>(Action<TUsing> tryAction, Action<Exception> CatchHandler1, Action<Exception> CatchHandler2, Action<Exception> CatchHandler3, Action<TUsing> FinallyAction, TUsing arg)
            where TE1 : Exception
            where TE2 : Exception
            where TE3 : Exception
            where TUsing : IDisposable
        {
            using (arg)
            {
                try
                {
                    tryAction(arg);
                }
                catch (TE1 t)
                {
                    CatchHandler1(t);
                }
                catch (TE2 t)
                {
                    CatchHandler2(t);
                }
                catch (TE3 t)
                {
                    CatchHandler3(t);
                }
                finally
                {
                    FinallyAction(arg);
                }
            }
        }

        public static void TryCatchFinally<TArg, TE>(Action<TArg> tryAction, TArg arg, Action<Exception> CatchAction, Action FinallyAction)
            where TE : Exception
        {
            try
            {
                tryAction(arg);
            }
            catch (TE t)
            {
                CatchAction(t);
            }
            finally
            {
                FinallyAction();
            }
        }

        public static void TryCatch<T1>(Action tryAction, Action<Exception> handler, Action FinallyAction)
            where T1 : Exception
        {
            try
            {
                tryAction();
            }
            catch (T1 ex)
            {
                handler(ex);
            }
            finally
            {
                FinallyAction();
            }
        }

        public static void TryCatchFinally<T1, TF>(Action tryAction, Action<Exception> handler, Action<TF> finallyAction, TF FinallyArg)
            where T1 : Exception
        {
            try
            {
                tryAction();
            }
            catch (T1 ex)
            {
                handler(ex);
            }
            finally
            {
                finallyAction(FinallyArg);
            }
        }

        public static void TryCatchFinally<TE1, TE2, TF>(Action tryAction, Action<Exception> handler1, Action<Exception> handler2, Action<TF> finallyAction, TF FinallyArg)
            where TE1 : Exception
            where TE2 : Exception
        {
            try
            {
                tryAction();
            }
            catch (TE1 ex)
            {
                handler1(ex);
            }
            catch (TE2 ex)
            {
                handler2(ex);
            }
            finally
            {
                finallyAction(FinallyArg);
            }
        }


        public static void TryCatchFinally<TE1, TE2, TE3, TF>(Action tryAction, Action<Exception> handler1, Action<Exception> handler2, Action<Exception> handler3, Action<TF> finallyAction, TF FinallyArg)
            where TE1 : Exception
            where TE2 : Exception
            where TE3 : Exception
        {
            try
            {
                tryAction();
            }
            catch (TE1 ex)
            {
                handler1(ex);
            }
            catch (TE2 ex)
            {
                handler2(ex);
            }
            catch (TE3 ex)
            {
                handler3(ex);
            }
            finally
            {
                finallyAction(FinallyArg);
            }
        }
    }
}
added 888 characters in body; edited tags; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Functional Exceptionexception handling with TryCatchFinally Statementstatement helpers

Hello All, I wrote something to the effect of a try catch finallytry catch finally statement helper in functional style so that I can shorten much of the code I'm writing for my Service Layer that connects to a sql database. Because of that I found myself needing to code up allot of try catch statements. I found this article on a functional exception handling and looked at the authors bitbucket source and was inspired to write the following with a differing style from that of the author but, suiting my purposes. I'm certainly not an expert and I could be totally wrong in my approach here. Looking for yes or no and here is why. Thoughts.....SQL database.

Because of that, I found myself needing to code up a lot of try catch statements. I found this article this article on a functional exception handling and looked at the author's Bitbucket source and was inspired to write the following with a differing style from that of the author but, suiting my purposes. I'm certainly not an expert and I could be totally wrong in my approach here. I'm looking for "yes" or "no" and "here is why".

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common.Helper { public static class FunctionalHelpers { public static void TryCatch<TE>(Action tryAction, Action<Exception> handler) where TE : Exception { try { tryAction(); } catch (TE ex) { handler(ex); } }

    public static void TryCatch&lt;TArg, TE&gt;(Action&lt;TArg&gt; tryAction, TArg args, Action&lt;Exception&gt; handler)
      where TE : Exception
    {
        try { tryAction(args); }
        catch (TE ex)
        {
            handler(ex);
        }
    }

    public static void TryCatchFinally&lt;TE&gt;(Action tryAction, Action&lt;Exception&gt; CatchAction, Action FinallyAction)
        where TE : Exception
    {
        try
        {
            tryAction();
        }
        catch (TE t)
        {
            CatchAction(t);
        }
        finally
        {
            FinallyAction();
        }
    }

    public static void TryCatchFinallyUsing&lt;TE, TUsing&gt;(Action&lt;TUsing&gt; tryAction, Action&lt;Exception&gt; CatchAction, Action&lt;TUsing&gt; FinallyAction, TUsing arg)
    where TE : Exception
    where TUsing : IDisposable
    {
        using (arg)
        {
            try
            {
                tryAction(arg);
            }
            catch (TE t)
            {
                CatchAction(t);
            }
            finally
            {
                FinallyAction(arg);
            }
        }
    }

    public static void TryCatchFinallyUsing&lt;TE1, TE2, TUsing&gt;(Action&lt;TUsing&gt; tryAction, Action&lt;TE1&gt; CatchHandler1, Action&lt;TE2&gt; CatchHandler2, Action&lt;TUsing&gt; FinallyAction, TUsing arg)
        where TE1 : Exception
        where TE2 : Exception
        where TUsing : IDisposable
    {
        using (arg)
        {
            try
            {
                tryAction(arg);
            }
            catch (TE1 t)
            {
                CatchHandler1(t);
            }
            catch (TE2 t)
            {
                CatchHandler2(t);
            }
            finally
            {
                FinallyAction(arg);
            }
        }
    }

    public static void TryCatchFinallyUsing&lt;TE1, TE2, TE3, TUsing&gt;(Action&lt;TUsing&gt; tryAction, Action&lt;Exception&gt; CatchHandler1, Action&lt;Exception&gt; CatchHandler2, Action&lt;Exception&gt; CatchHandler3, Action&lt;TUsing&gt; FinallyAction, TUsing arg)
        where TE1 : Exception
        where TE2 : Exception
        where TE3 : Exception
        where TUsing : IDisposable
    {
        using (arg)
        {
            try
            {
                tryAction(arg);
            }
            catch (TE1 t)
            {
                CatchHandler1(t);
            }
            catch (TE2 t)
            {
                CatchHandler2(t);
            }
            catch (TE3 t)
            {
                CatchHandler3(t);
            }
            finally
            {
                FinallyAction(arg);
            }
        }
    }

    public static void TryCatchFinally&lt;TArg, TE&gt;(Action&lt;TArg&gt; tryAction, TArg arg, Action&lt;Exception&gt; CatchAction, Action FinallyAction)
        where TE : Exception
    {
        try
        {
            tryAction(arg);
        }
        catch (TE t)
        {
            CatchAction(t);
        }
        finally
        {
            FinallyAction();
        }
    }

    public static void TryCatch&lt;T1&gt;(Action tryAction, Action&lt;Exception&gt; handler, Action FinallyAction)
        where T1 : Exception
    {
        try
        {
            tryAction();
        }
        catch (T1 ex)
        {
            handler(ex);
        }
        finally
        {
            FinallyAction();
        }
    }

    public static void TryCatchFinally&lt;T1, TF&gt;(Action tryAction, Action&lt;Exception&gt; handler, Action&lt;TF&gt; finallyAction, TF FinallyArg)
        where T1 : Exception
    {
        try
        {
            tryAction();
        }
        catch (T1 ex)
        {
            handler(ex);
        }
        finally
        {
            finallyAction(FinallyArg);
        }
    }

    public static void TryCatchFinally&lt;TE1, TE2, TF&gt;(Action tryAction, Action&lt;Exception&gt; handler1, Action&lt;Exception&gt; handler2, Action&lt;TF&gt; finallyAction, TF FinallyArg)
        where TE1 : Exception
        where TE2 : Exception
    {
        try
        {
            tryAction();
        }
        catch (TE1 ex)
        {
            handler1(ex);
        }
        catch (TE2 ex)
        {
            handler2(ex);
        }
        finally
        {
            finallyAction(FinallyArg);
        }
    }


    public static void TryCatchFinally&lt;TE1, TE2, TE3, TF&gt;(Action tryAction, Action&lt;Exception&gt; handler1, Action&lt;Exception&gt; handler2, Action&lt;Exception&gt; handler3, Action&lt;TF&gt; finallyAction, TF FinallyArg)
        where TE1 : Exception
        where TE2 : Exception
        where TE3 : Exception
    {
        try
        {
            tryAction();
        }
        catch (TE1 ex)
        {
            handler1(ex);
        }
        catch (TE2 ex)
        {
            handler2(ex);
        }
        catch (TE3 ex)
        {
            handler3(ex);
        }
        finally
        {
            finallyAction(FinallyArg);
        }
    }
}

}

namespace Common.Helper { public static class FunctionalHelpers { public static void TryCatch&lt;TE&gt;(Action tryAction, Action&lt;Exception&gt; handler) where TE : Exception { try { tryAction(); } catch (TE ex) { handler(ex); } } public static void TryCatch&lt;TArg, TE&gt;(Action&lt;TArg&gt; tryAction, TArg args, Action&lt;Exception&gt; handler) where TE : Exception { try { tryAction(args); } catch (TE ex) { handler(ex); } } public static void TryCatchFinally&lt;TE&gt;(Action tryAction, Action&lt;Exception&gt; CatchAction, Action FinallyAction) where TE : Exception { try { tryAction(); } catch (TE t) { CatchAction(t); } finally { FinallyAction(); } } public static void TryCatchFinallyUsing&lt;TE, TUsing&gt;(Action&lt;TUsing&gt; tryAction, Action&lt;Exception&gt; CatchAction, Action&lt;TUsing&gt; FinallyAction, TUsing arg) where TE : Exception where TUsing : IDisposable { using (arg) { try { tryAction(arg); } catch (TE t) { CatchAction(t); } finally { FinallyAction(arg); } } } public static void TryCatchFinallyUsing&lt;TE1, TE2, TUsing&gt;(Action&lt;TUsing&gt; tryAction, Action&lt;TE1&gt; CatchHandler1, Action&lt;TE2&gt; CatchHandler2, Action&lt;TUsing&gt; FinallyAction, TUsing arg) where TE1 : Exception where TE2 : Exception where TUsing : IDisposable { using (arg) { try { tryAction(arg); } catch (TE1 t) { CatchHandler1(t); } catch (TE2 t) { CatchHandler2(t); } finally { FinallyAction(arg); } } } public static void TryCatchFinallyUsing&lt;TE1, TE2, TE3, TUsing&gt;(Action&lt;TUsing&gt; tryAction, Action&lt;Exception&gt; CatchHandler1, Action&lt;Exception&gt; CatchHandler2, Action&lt;Exception&gt; CatchHandler3, Action&lt;TUsing&gt; FinallyAction, TUsing arg) where TE1 : Exception where TE2 : Exception where TE3 : Exception where TUsing : IDisposable { using (arg) { try { tryAction(arg); } catch (TE1 t) { CatchHandler1(t); } catch (TE2 t) { CatchHandler2(t); } catch (TE3 t) { CatchHandler3(t); } finally { FinallyAction(arg); } } } public static void TryCatchFinally&lt;TArg, TE&gt;(Action&lt;TArg&gt; tryAction, TArg arg, Action&lt;Exception&gt; CatchAction, Action FinallyAction) where TE : Exception { try { tryAction(arg); } catch (TE t) { CatchAction(t); } finally { FinallyAction(); } } public static void TryCatch&lt;T1&gt;(Action tryAction, Action&lt;Exception&gt; handler, Action FinallyAction) where T1 : Exception { try { tryAction(); } catch (T1 ex) { handler(ex); } finally { FinallyAction(); } } public static void TryCatchFinally&lt;T1, TF&gt;(Action tryAction, Action&lt;Exception&gt; handler, Action&lt;TF&gt; finallyAction, TF FinallyArg) where T1 : Exception { try { tryAction(); } catch (T1 ex) { handler(ex); } finally { finallyAction(FinallyArg); } } public static void TryCatchFinally&lt;TE1, TE2, TF&gt;(Action tryAction, Action&lt;Exception&gt; handler1, Action&lt;Exception&gt; handler2, Action&lt;TF&gt; finallyAction, TF FinallyArg) where TE1 : Exception where TE2 : Exception { try { tryAction(); } catch (TE1 ex) { handler1(ex); } catch (TE2 ex) { handler2(ex); } finally { finallyAction(FinallyArg); } } public static void TryCatchFinally&lt;TE1, TE2, TE3, TF&gt;(Action tryAction, Action&lt;Exception&gt; handler1, Action&lt;Exception&gt; handler2, Action&lt;Exception&gt; handler3, Action&lt;TF&gt; finallyAction, TF FinallyArg) where TE1 : Exception where TE2 : Exception where TE3 : Exception { try { tryAction(); } catch (TE1 ex) { handler1(ex); } catch (TE2 ex) { handler2(ex); } catch (TE3 ex) { handler3(ex); } finally { finallyAction(FinallyArg); } } } }

 

Functional Exception handling with TryCatchFinally Statement helpers

Hello All, I wrote something to the effect of a try catch finally statement helper in functional style so that I can shorten much of the code I'm writing for my Service Layer that connects to a sql database. Because of that I found myself needing to code up allot of try catch statements. I found this article on a functional exception handling and looked at the authors bitbucket source and was inspired to write the following with a differing style from that of the author but, suiting my purposes. I'm certainly not an expert and I could be totally wrong in my approach here. Looking for yes or no and here is why. Thoughts......

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common.Helper { public static class FunctionalHelpers { public static void TryCatch<TE>(Action tryAction, Action<Exception> handler) where TE : Exception { try { tryAction(); } catch (TE ex) { handler(ex); } }

    public static void TryCatch&lt;TArg, TE&gt;(Action&lt;TArg&gt; tryAction, TArg args, Action&lt;Exception&gt; handler)
      where TE : Exception
    {
        try { tryAction(args); }
        catch (TE ex)
        {
            handler(ex);
        }
    }

    public static void TryCatchFinally&lt;TE&gt;(Action tryAction, Action&lt;Exception&gt; CatchAction, Action FinallyAction)
        where TE : Exception
    {
        try
        {
            tryAction();
        }
        catch (TE t)
        {
            CatchAction(t);
        }
        finally
        {
            FinallyAction();
        }
    }

    public static void TryCatchFinallyUsing&lt;TE, TUsing&gt;(Action&lt;TUsing&gt; tryAction, Action&lt;Exception&gt; CatchAction, Action&lt;TUsing&gt; FinallyAction, TUsing arg)
    where TE : Exception
    where TUsing : IDisposable
    {
        using (arg)
        {
            try
            {
                tryAction(arg);
            }
            catch (TE t)
            {
                CatchAction(t);
            }
            finally
            {
                FinallyAction(arg);
            }
        }
    }

    public static void TryCatchFinallyUsing&lt;TE1, TE2, TUsing&gt;(Action&lt;TUsing&gt; tryAction, Action&lt;TE1&gt; CatchHandler1, Action&lt;TE2&gt; CatchHandler2, Action&lt;TUsing&gt; FinallyAction, TUsing arg)
        where TE1 : Exception
        where TE2 : Exception
        where TUsing : IDisposable
    {
        using (arg)
        {
            try
            {
                tryAction(arg);
            }
            catch (TE1 t)
            {
                CatchHandler1(t);
            }
            catch (TE2 t)
            {
                CatchHandler2(t);
            }
            finally
            {
                FinallyAction(arg);
            }
        }
    }

    public static void TryCatchFinallyUsing&lt;TE1, TE2, TE3, TUsing&gt;(Action&lt;TUsing&gt; tryAction, Action&lt;Exception&gt; CatchHandler1, Action&lt;Exception&gt; CatchHandler2, Action&lt;Exception&gt; CatchHandler3, Action&lt;TUsing&gt; FinallyAction, TUsing arg)
        where TE1 : Exception
        where TE2 : Exception
        where TE3 : Exception
        where TUsing : IDisposable
    {
        using (arg)
        {
            try
            {
                tryAction(arg);
            }
            catch (TE1 t)
            {
                CatchHandler1(t);
            }
            catch (TE2 t)
            {
                CatchHandler2(t);
            }
            catch (TE3 t)
            {
                CatchHandler3(t);
            }
            finally
            {
                FinallyAction(arg);
            }
        }
    }

    public static void TryCatchFinally&lt;TArg, TE&gt;(Action&lt;TArg&gt; tryAction, TArg arg, Action&lt;Exception&gt; CatchAction, Action FinallyAction)
        where TE : Exception
    {
        try
        {
            tryAction(arg);
        }
        catch (TE t)
        {
            CatchAction(t);
        }
        finally
        {
            FinallyAction();
        }
    }

    public static void TryCatch&lt;T1&gt;(Action tryAction, Action&lt;Exception&gt; handler, Action FinallyAction)
        where T1 : Exception
    {
        try
        {
            tryAction();
        }
        catch (T1 ex)
        {
            handler(ex);
        }
        finally
        {
            FinallyAction();
        }
    }

    public static void TryCatchFinally&lt;T1, TF&gt;(Action tryAction, Action&lt;Exception&gt; handler, Action&lt;TF&gt; finallyAction, TF FinallyArg)
        where T1 : Exception
    {
        try
        {
            tryAction();
        }
        catch (T1 ex)
        {
            handler(ex);
        }
        finally
        {
            finallyAction(FinallyArg);
        }
    }

    public static void TryCatchFinally&lt;TE1, TE2, TF&gt;(Action tryAction, Action&lt;Exception&gt; handler1, Action&lt;Exception&gt; handler2, Action&lt;TF&gt; finallyAction, TF FinallyArg)
        where TE1 : Exception
        where TE2 : Exception
    {
        try
        {
            tryAction();
        }
        catch (TE1 ex)
        {
            handler1(ex);
        }
        catch (TE2 ex)
        {
            handler2(ex);
        }
        finally
        {
            finallyAction(FinallyArg);
        }
    }


    public static void TryCatchFinally&lt;TE1, TE2, TE3, TF&gt;(Action tryAction, Action&lt;Exception&gt; handler1, Action&lt;Exception&gt; handler2, Action&lt;Exception&gt; handler3, Action&lt;TF&gt; finallyAction, TF FinallyArg)
        where TE1 : Exception
        where TE2 : Exception
        where TE3 : Exception
    {
        try
        {
            tryAction();
        }
        catch (TE1 ex)
        {
            handler1(ex);
        }
        catch (TE2 ex)
        {
            handler2(ex);
        }
        catch (TE3 ex)
        {
            handler3(ex);
        }
        finally
        {
            finallyAction(FinallyArg);
        }
    }
}

}

 

Functional exception handling with TryCatchFinally statement helpers

I wrote something to the effect of a try catch finally statement helper in functional style so that I can shorten much of the code I'm writing for my Service Layer that connects to an SQL database.

Because of that, I found myself needing to code up a lot of try catch statements. I found this article this article on a functional exception handling and looked at the author's Bitbucket source and was inspired to write the following with a differing style from that of the author but, suiting my purposes. I'm certainly not an expert and I could be totally wrong in my approach here. I'm looking for "yes" or "no" and "here is why".

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common.Helper
{
    public static class FunctionalHelpers
    {
        public static void TryCatch&lt;TE&gt;(Action tryAction, Action&lt;Exception&gt; handler)
          where TE : Exception
        {
            try { tryAction(); }
            catch (TE ex)
            {
                handler(ex);
            }
        }

        public static void TryCatch&lt;TArg, TE&gt;(Action&lt;TArg&gt; tryAction, TArg args, Action&lt;Exception&gt; handler)
          where TE : Exception
        {
            try { tryAction(args); }
            catch (TE ex)
            {
                handler(ex);
            }
        }

        public static void TryCatchFinally&lt;TE&gt;(Action tryAction, Action&lt;Exception&gt; CatchAction, Action FinallyAction)
            where TE : Exception
        {
            try
            {
                tryAction();
            }
            catch (TE t)
            {
                CatchAction(t);
            }
            finally
            {
                FinallyAction();
            }
        }

        public static void TryCatchFinallyUsing&lt;TE, TUsing&gt;(Action&lt;TUsing&gt; tryAction, Action&lt;Exception&gt; CatchAction, Action&lt;TUsing&gt; FinallyAction, TUsing arg)
        where TE : Exception
        where TUsing : IDisposable
        {
            using (arg)
            {
                try
                {
                    tryAction(arg);
                }
                catch (TE t)
                {
                    CatchAction(t);
                }
                finally
                {
                    FinallyAction(arg);
                }
            }
        }

        public static void TryCatchFinallyUsing&lt;TE1, TE2, TUsing&gt;(Action&lt;TUsing&gt; tryAction, Action&lt;TE1&gt; CatchHandler1, Action&lt;TE2&gt; CatchHandler2, Action&lt;TUsing&gt; FinallyAction, TUsing arg)
            where TE1 : Exception
            where TE2 : Exception
            where TUsing : IDisposable
        {
            using (arg)
            {
                try
                {
                    tryAction(arg);
                }
                catch (TE1 t)
                {
                    CatchHandler1(t);
                }
                catch (TE2 t)
                {
                    CatchHandler2(t);
                }
                finally
                {
                    FinallyAction(arg);
                }
            }
        }

        public static void TryCatchFinallyUsing&lt;TE1, TE2, TE3, TUsing&gt;(Action&lt;TUsing&gt; tryAction, Action&lt;Exception&gt; CatchHandler1, Action&lt;Exception&gt; CatchHandler2, Action&lt;Exception&gt; CatchHandler3, Action&lt;TUsing&gt; FinallyAction, TUsing arg)
            where TE1 : Exception
            where TE2 : Exception
            where TE3 : Exception
            where TUsing : IDisposable
        {
            using (arg)
            {
                try
                {
                    tryAction(arg);
                }
                catch (TE1 t)
                {
                    CatchHandler1(t);
                }
                catch (TE2 t)
                {
                    CatchHandler2(t);
                }
                catch (TE3 t)
                {
                    CatchHandler3(t);
                }
                finally
                {
                    FinallyAction(arg);
                }
            }
        }

        public static void TryCatchFinally&lt;TArg, TE&gt;(Action&lt;TArg&gt; tryAction, TArg arg, Action&lt;Exception&gt; CatchAction, Action FinallyAction)
            where TE : Exception
        {
            try
            {
                tryAction(arg);
            }
            catch (TE t)
            {
                CatchAction(t);
            }
            finally
            {
                FinallyAction();
            }
        }

        public static void TryCatch&lt;T1&gt;(Action tryAction, Action&lt;Exception&gt; handler, Action FinallyAction)
            where T1 : Exception
        {
            try
            {
                tryAction();
            }
            catch (T1 ex)
            {
                handler(ex);
            }
            finally
            {
                FinallyAction();
            }
        }

        public static void TryCatchFinally&lt;T1, TF&gt;(Action tryAction, Action&lt;Exception&gt; handler, Action&lt;TF&gt; finallyAction, TF FinallyArg)
            where T1 : Exception
        {
            try
            {
                tryAction();
            }
            catch (T1 ex)
            {
                handler(ex);
            }
            finally
            {
                finallyAction(FinallyArg);
            }
        }

        public static void TryCatchFinally&lt;TE1, TE2, TF&gt;(Action tryAction, Action&lt;Exception&gt; handler1, Action&lt;Exception&gt; handler2, Action&lt;TF&gt; finallyAction, TF FinallyArg)
            where TE1 : Exception
            where TE2 : Exception
        {
            try
            {
                tryAction();
            }
            catch (TE1 ex)
            {
                handler1(ex);
            }
            catch (TE2 ex)
            {
                handler2(ex);
            }
            finally
            {
                finallyAction(FinallyArg);
            }
        }


        public static void TryCatchFinally&lt;TE1, TE2, TE3, TF&gt;(Action tryAction, Action&lt;Exception&gt; handler1, Action&lt;Exception&gt; handler2, Action&lt;Exception&gt; handler3, Action&lt;TF&gt; finallyAction, TF FinallyArg)
            where TE1 : Exception
            where TE2 : Exception
            where TE3 : Exception
        {
            try
            {
                tryAction();
            }
            catch (TE1 ex)
            {
                handler1(ex);
            }
            catch (TE2 ex)
            {
                handler2(ex);
            }
            catch (TE3 ex)
            {
                handler3(ex);
            }
            finally
            {
                finallyAction(FinallyArg);
            }
        }
    }
}
Tweeted twitter.com/#!/StackCodeReview/status/307936063538745344
Source Link
Terrance
  • 783
  • 1
  • 6
  • 8

Functional Exception handling with TryCatchFinally Statement helpers

Hello All, I wrote something to the effect of a try catch finally statement helper in functional style so that I can shorten much of the code I'm writing for my Service Layer that connects to a sql database. Because of that I found myself needing to code up allot of try catch statements. I found this article on a functional exception handling and looked at the authors bitbucket source and was inspired to write the following with a differing style from that of the author but, suiting my purposes. I'm certainly not an expert and I could be totally wrong in my approach here. Looking for yes or no and here is why. Thoughts......

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common.Helper { public static class FunctionalHelpers { public static void TryCatch<TE>(Action tryAction, Action<Exception> handler) where TE : Exception { try { tryAction(); } catch (TE ex) { handler(ex); } }

    public static void TryCatch&lt;TArg, TE&gt;(Action&lt;TArg&gt; tryAction, TArg args, Action&lt;Exception&gt; handler)
      where TE : Exception
    {
        try { tryAction(args); }
        catch (TE ex)
        {
            handler(ex);
        }
    }

    public static void TryCatchFinally&lt;TE&gt;(Action tryAction, Action&lt;Exception&gt; CatchAction, Action FinallyAction)
        where TE : Exception
    {
        try
        {
            tryAction();
        }
        catch (TE t)
        {
            CatchAction(t);
        }
        finally
        {
            FinallyAction();
        }
    }

    public static void TryCatchFinallyUsing&lt;TE, TUsing&gt;(Action&lt;TUsing&gt; tryAction, Action&lt;Exception&gt; CatchAction, Action&lt;TUsing&gt; FinallyAction, TUsing arg)
    where TE : Exception
    where TUsing : IDisposable
    {
        using (arg)
        {
            try
            {
                tryAction(arg);
            }
            catch (TE t)
            {
                CatchAction(t);
            }
            finally
            {
                FinallyAction(arg);
            }
        }
    }

    public static void TryCatchFinallyUsing&lt;TE1, TE2, TUsing&gt;(Action&lt;TUsing&gt; tryAction, Action&lt;TE1&gt; CatchHandler1, Action&lt;TE2&gt; CatchHandler2, Action&lt;TUsing&gt; FinallyAction, TUsing arg)
        where TE1 : Exception
        where TE2 : Exception
        where TUsing : IDisposable
    {
        using (arg)
        {
            try
            {
                tryAction(arg);
            }
            catch (TE1 t)
            {
                CatchHandler1(t);
            }
            catch (TE2 t)
            {
                CatchHandler2(t);
            }
            finally
            {
                FinallyAction(arg);
            }
        }
    }

    public static void TryCatchFinallyUsing&lt;TE1, TE2, TE3, TUsing&gt;(Action&lt;TUsing&gt; tryAction, Action&lt;Exception&gt; CatchHandler1, Action&lt;Exception&gt; CatchHandler2, Action&lt;Exception&gt; CatchHandler3, Action&lt;TUsing&gt; FinallyAction, TUsing arg)
        where TE1 : Exception
        where TE2 : Exception
        where TE3 : Exception
        where TUsing : IDisposable
    {
        using (arg)
        {
            try
            {
                tryAction(arg);
            }
            catch (TE1 t)
            {
                CatchHandler1(t);
            }
            catch (TE2 t)
            {
                CatchHandler2(t);
            }
            catch (TE3 t)
            {
                CatchHandler3(t);
            }
            finally
            {
                FinallyAction(arg);
            }
        }
    }

    public static void TryCatchFinally&lt;TArg, TE&gt;(Action&lt;TArg&gt; tryAction, TArg arg, Action&lt;Exception&gt; CatchAction, Action FinallyAction)
        where TE : Exception
    {
        try
        {
            tryAction(arg);
        }
        catch (TE t)
        {
            CatchAction(t);
        }
        finally
        {
            FinallyAction();
        }
    }

    public static void TryCatch&lt;T1&gt;(Action tryAction, Action&lt;Exception&gt; handler, Action FinallyAction)
        where T1 : Exception
    {
        try
        {
            tryAction();
        }
        catch (T1 ex)
        {
            handler(ex);
        }
        finally
        {
            FinallyAction();
        }
    }

    public static void TryCatchFinally&lt;T1, TF&gt;(Action tryAction, Action&lt;Exception&gt; handler, Action&lt;TF&gt; finallyAction, TF FinallyArg)
        where T1 : Exception
    {
        try
        {
            tryAction();
        }
        catch (T1 ex)
        {
            handler(ex);
        }
        finally
        {
            finallyAction(FinallyArg);
        }
    }

    public static void TryCatchFinally&lt;TE1, TE2, TF&gt;(Action tryAction, Action&lt;Exception&gt; handler1, Action&lt;Exception&gt; handler2, Action&lt;TF&gt; finallyAction, TF FinallyArg)
        where TE1 : Exception
        where TE2 : Exception
    {
        try
        {
            tryAction();
        }
        catch (TE1 ex)
        {
            handler1(ex);
        }
        catch (TE2 ex)
        {
            handler2(ex);
        }
        finally
        {
            finallyAction(FinallyArg);
        }
    }


    public static void TryCatchFinally&lt;TE1, TE2, TE3, TF&gt;(Action tryAction, Action&lt;Exception&gt; handler1, Action&lt;Exception&gt; handler2, Action&lt;Exception&gt; handler3, Action&lt;TF&gt; finallyAction, TF FinallyArg)
        where TE1 : Exception
        where TE2 : Exception
        where TE3 : Exception
    {
        try
        {
            tryAction();
        }
        catch (TE1 ex)
        {
            handler1(ex);
        }
        catch (TE2 ex)
        {
            handler2(ex);
        }
        catch (TE3 ex)
        {
            handler3(ex);
        }
        finally
        {
            finallyAction(FinallyArg);
        }
    }
}

}