Search code examples
c#debuggingerror-handlingmessagebox

C# MessageBox Error Messages


In my application I am using message boxes to display error information.

try
{
   // Something...
}
catch (SystemException ex)
{
   MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

This was fine at first, but as my program grows it becomes increasingly difficult to find the try-catch block where this error is generated. Is there a way to display the line of code or function in which the error was generated? I am using Microsoft Visual C# 2008 Express Edition. Thanks.


Solution

  • This will give you a LOT of information about the method that caused the error (the stacktrace)

    MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);