views:

9

answers:

1

In my current error handling, I specify the Form, function and error message in a messagebox similar to the following.

try
{
  //Some code here
}
catch(Exception ex)
{
     MessageBox.Show("Form Title : " + this.Title + "\nFunction : CurrentFunction \nError : " + ex.Message);
     return;
}

This works for me, but was curious if I can make the process even more simple, and generate the function name, instead of typing it out every time I want to display the error message.

Additionally: I know you can include the stacktrace and view the top few lines, but I was curious if there was a cleaner way to show the function.

+1  A: 

Yes, if you just need the current function (not the calling function), you can use MethodBase.GetCurrentMethod:

string currentMethod = System.Reflection.MethodBase.GetCurrentMethod().Name;
Chris Schmich
That was exactly what I was looking for. Thank you
Redburn