We help IT Professionals succeed at work.

debugging tricks? WinForms

I'm trying centralize my TRY/CATCH errors into one location (EG - one call) I want to auto-capture the Form name and the Procedure name, at the MOMENT the error happens.

Any idea's how (other than hard code in EVERY Try/Catch block)?

See ???????????? below.

        Try
            Dim x() As Single   'set up forced error
            x(14) = (1 / 0)     'force error
        Catch ex As Exception
            Dim sbex As StringBuilder = New StringBuilder()
            sbex.Append(ex.ToString())
            sbex.Append(vbCrLf & "FormName=" & ????????????)
            sbex.Append(vbCrLf & "ProcName=" & ????????????)
            MessageBox.Show(sbex.ToString())
        End Try

Open in new window

Comment
Watch Question

Vel EousResearch & Development Manager
Commented:
Take a look at the Stacktrace class:  http://msdn.microsoft.com/en-us/library/system.diagnostics.stacktrace.aspx#Y0

Specifically look at the overloaded constructor that accepts and exception object, and the additional boolean for capturing source information:  http://msdn.microsoft.com/en-us/library/dsay49kt.aspx

Author

Commented:
@Tchuki

Almost ... a good answer - but doesn't work unless application is compiled with the Debug configuration.

If I don't get any other answers then I'll accept yours. I'd like a way to capture when compiled with Release configuration.

But my answer may simple be:  CAN'T BE DONE in Release configuration without hard code in every Try/Catch
Top Expert 2011

Commented:
If I understand you correctly, then replace

            MessageBox.Show(sbex.ToString())

with

   Response.Write(sbex.ToString())

You can response. end if you wish to stop and view current values
    //Response.End();
Most Valuable Expert 2012
Top Expert 2014
Commented:
Use Me.Text to get the form name. Method name is another story

http://www.codeproject.com/KB/dotnet/MethodName.aspx
Most Valuable Expert 2012
Top Expert 2014

Commented:
@sammySeltzer
Read the question title!
Vel EousResearch & Development Manager

Commented:
@Volking

Stacktrace does work under release builds just at a reduced capacity, however, can still prove very useful in pointing you in the right direction.  I appreciate though that "in the right direction" might not be quite what you are looking for.

As a test take a look at the code snippet attached, give it a try in your app then force an exception.  See if it provides desired, and accurate, exception reporting for you.
public void WriteToFile(Exception ex)
{
  myFileWriter.WriteLine("Oops!: " + ex.Message.ToString());
  System.Diagnostics.StackTrace trace As new System.Diagnostics.StackTrace(ex, true);
  myFileWriter.WriteLine("Operation: " + trace.GetFrame(trace.GetFrames().Count() - 1).GetMethod().Name.ToString());
  myFileWriter.WriteLine("Line: " + trace.GetFrame(trace.GetFrames().Count() - 1).GetFileLineNumber());
  myFileWriter.WriteLine("Column: " + trace.GetFrame(trace.GetFrames().Count() - 1).GetFileColumnNumber());
}

Open in new window

Top Expert 2015
Commented:
As specified sooner, Me.Name will give you the name of the Form, Module or Class.

The following will give you the name of the method in which it is callde, even in Release, unless the code has been obfuscated.
New StackTrace().GetFrame(0).GetMethod().Name()

Open in new window

If you want to get the name of the method that called the current one, in a centralized method that is called when there is an error, get the following entry in the StackTrace:
New StackTrace().GetFrame(1).GetMethod().Name()

Open in new window