mardi 28 février 2017

Get Line Number of Exception from Parameterized Action

Whenever I pass an Action as a parameter that is declared inline I don't get the correct line number when an exception is thrown. For example, if I declare this function:

public static void Do(Action action)
{
    //do some things
    action();
    //do some things
}

And then call it like this:

Helper.Do(() => {
   int x = 1;
   x++;
   x = x / 0;
   x--;
});

The exception that returns will reference the line Helper.Do(() => { which doesn't tell me which line threw the actual error. Is there a way to retrieve the correct line number while still using this pattern?

Aucun commentaire:

Enregistrer un commentaire