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<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);
}
}
}
}