0
votes

I have a C# program with multiple methods with different parameters and different return types, but most of them have a try/catch block with the same exception logic handling. There are multiple catch statement for all methods, but the logic inside them is the same for all methods.

Is there a way to create a helper method so I won't have duplicate code in the catch ?

    public static async Task<long> Method1(string requestedKey)
    {
        try
        {
            //code
            return value;
        }
        catch (Exception1 ex)
        {
            Exception1Handler(ex);
        }
        catch (Exception2 ex)
        {
            Exception2Handler(ex);
        }
        catch (Exception3 ex)
        {
            Exception3Handler(ex);
        }
        catch (Exception ex)
        {               
        }

        return 0;
    }


    public static async Task<T> Method2(string key, string value)
    {
        try
        {
            //code
            return value;
        }
        catch (Exception1 ex)
        {
            Exception1Handler(ex);
        }
        catch (Exception2 ex)
        {
            Exception2Handler(ex);
        }
        catch (Exception3 ex)
        {
            Exception3Handler(ex);
        }
        catch (Exception ex)
        {               
        }

         return default(T);
    }

How could I group the catch statements in a helper method, so that I do not have duplicate code?

2
Yes there is. If you would add an example of your code, I am sure someone will take the time to give you an example of how to do it. - Fildor
"create a helper method" That´s exactly what you should do. Do you have any problems doing this? What did you try and where exactly are you stuck? - MakePeaceGreatAgain
Yes - remove all try .. catch blocks. Handle only exceptions you want to handle, If exceptions can happened because of user's input, do validation instead of throwing exception. Otherwise catch all of them on top layer and provide friendly message to the user. - Fabio
Does your code look something like the code in this question? If so, answers there should apply. If not, some concrete examples of what your code does look like would help immensely. - Damien_The_Unbeliever

2 Answers

2
votes

If the idea is to avoid duplicating catch blocks, you can use the following approach

public void HandleException(Action action)
{
    try
    {
        action();
    }
    catch (Exception1 e)
    {
        // Exception1 handling
    }
    catch (Exception2 e)
    {
        // Exception2 handling
    }
    catch (Exception3 e)
    {
        // Exception3 handling
    }
    catch (Exception e)
    {
        // Exception handling
    }
}

...

HandleException(() => /* implementation */)
0
votes

Instead of having this :

void AA(string B)
{
    try
    {
        //Do something
    }
    catch (Exception)
    {
        //Do something else
    }
}

void AA(string B, string C)
{
    try
    {
        //Do something
    }
    catch (Exception)
    {
        //Do something else
    }
}

and type the same exception over and over(I assume they will exactly same since you say so in your question) you can do something like this :

void CallerMethod()
{
    try
    {
        AA("");
        AA("", "");
    }
    catch (Exception)
    {
        //My only one exception
    }
}

void AA(string B)
{
    //Do something
}

void AA(string B, string C)
{
    //Do something
}

This way if any of your overloaded method will throw an exception you can handle them from only 1 place