Skip to main content
1 of 3
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);
        }
    }
}

}

Terrance
  • 783
  • 1
  • 6
  • 8