0

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";
            }
        }
    }   
Raj Karri
  • 551
  • 4
  • 19
  • `I am stuck with this error` What error is that? – Eric J. Mar 03 '16 at 23:33
  • It's giving compiler error "Delegate 'System.Func' does not take 1 arguments" – Raj Karri Mar 03 '16 at 23:37
  • "I am trying to implement global try/catch instead of writing try/catch on each and every method". Neither of those makes any sense. The first is pointless for anything except logging because its too general to deal with what's gone wrong. The latter is too wide since most calls shouldn't throw and many that could are best handled by letting it pass up to the caller in turn. – Jon Hanna Mar 03 '16 at 23:43
  • "instead of writing try/catch on each and every method" - You shouldn't do that at all. You should only ever catch an exception that you can sensibly handle. Have a read of Eric Lippert's [Vexing exceptions](https://blogs.msdn.microsoft.com/ericlippert/2008/09/10/vexing-exceptions/). – Enigmativity Mar 04 '16 at 00:24

3 Answers3

3
string s2 = GlobalTryCatch((x, y) => Sum(i, j), i, j);

The compiler couldn't match your original method with string GlobalTryCatch<T1>(Func<T1, string> action, T1 i) because your lambda expression only had one argument, but the method signature calls for two arguments. The fix is to use (x, y), which indicates that the lambda is taking two arguments.

As a shortcut, you can just provide the "method group", which would result in the following:

string s2 = GlobalTryCatch(Sum, i, j);
devuxer
  • 41,681
  • 47
  • 180
  • 292
1

You can provide your two-parameter Func like this

string s2 = GlobalTryCatch(Sum, i, j); // error here..

There is no need to add a lambda expression.

Eric J.
  • 147,927
  • 63
  • 340
  • 553
0

You might want to consider handling Application.UnhandledException event and handle your exceptions there.

Your method signatures differ. That is why you cannot use a single implementation of GlobalTryCatch.

Edin
  • 1,476
  • 11
  • 21