Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I move my exception code to a handling function. So I don't have to repeat same code

Tags:

c#

I currently handle my exceptions like this:

try {

        }
        catch (ServiceException ex) {
            ModelState.Merge(ex.Errors);
        }
        catch (Exception e) {
            Trace.Write(e);
            ModelState.AddModelError("", "Database access error: " + e.Message);
        }

This works but it's the same code I repeat many times. What I am looking for is some suggestion on how I could move this into an external function. I don't necessarily need to move the try block there but at least the other code.

Maybe a function that was passed the Exception and the ModelState (as a reference). Can anyone suggest a clean way that I could code up this function. I'm asking here because almost always someone seems to come up with a solution that I could never have thought of. Thanks Samantha.

like image 265
Samantha J T Star Avatar asked Dec 19 '11 14:12

Samantha J T Star


3 Answers

You could make a method that takes in an Action, and invokes it in a try/catch block:

private void RunAndHandleExceptions(Action action)
        {
            try
            {
                action.Invoke();
            }
            catch (ServiceException ex)
            {
                ModelState.Merge(ex.Errors);
            }
            catch (Exception e)
            {
                Trace.Write(e);
                ModelState.AddModelError("", "Database access error: " + e.Message);
            }
        }

And call it like this:

RunAndHandleExceptions(new Action(() =>
                {
                    //Do some computing
                }));

EDIT: with a parameter (example, can run in a console program):

private static void ParameterizedTask()
    {
        Task.Factory.StartNew(new Action<object>((y) =>
        {
            Console.WriteLine(y);
        }), 5);
        Thread.Sleep(1500);
    }
//OUTPUT: 5

For more info you can take a look at this thread.

like image 90
Louis Kottmann Avatar answered Oct 15 '22 08:10

Louis Kottmann


(Updated to match the OP's new requirement in comments)

private void HandleException(Action<IEnumerable<string>> action, 
  IEnumerable<string> parameters)
{
  try {
    action(parameters);
  }
  catch (ServiceException ex) {
    ModelState.Merge(ex.Errors);
  }
  catch (Exception e) {
    Trace.Write(e);
    ModelState.AddModelError("", "Database access error: " + e.Message);
  }
}

which can be invoked with a lambda for instance:

HandleException((parameters) => Console.WriteLine(parameters.FirstOrDefault()), 
                                new string[] {"Pretty safe in this case"});
like image 45
vc 74 Avatar answered Oct 15 '22 08:10

vc 74


Besides passing the the exception to another function as has been suggested in the comments, you could also pass the code to be run as an Action to function would then run the action in a try catch.

like image 31
Jacob Poul Richardt Avatar answered Oct 15 '22 08:10

Jacob Poul Richardt