I saw some solutions on stackoverflow for similar issues but looks like each problem is unique.
I am trying to implement global try/catch instead of writing try/catch on each and every method but I am stuck with this error. It's working fine for one argument and not working for methods taking more than one argument.
class Program
{
static void Main(string[] args)
{
int i = 5;
int j = 10;
string s1 = GlobalTryCatch(x => square(i), i);
string s2 = GlobalTryCatch(x => Sum(i,j), i, j); // error here..
Console.Read();
}
private static string square(int i)
{
Console.WriteLine(i * i);
return "1";
}
private static string Sum(int i, int j)
{
Console.WriteLine(i+j);
return "1";
}
private static string GlobalTryCatch<T1>(Func<T1, string> action, T1 i)
{
try
{
action.Invoke(i);
return "success";
}
catch (Exception e)
{
return "failure";
}
}
private static string GlobalTryCatch<T1, T2>(Func<T1, T2, string> action, T1 i, T2 j)
{
try
{
action.Invoke(i, j);
return "success";
}
catch (Exception e)
{
return "failure";
}
}
}