Home All Groups Group Topic Archive Search About

Finding name of exception method

Author
23 Mar 2010 9:46 AM
John
Hi

Is it possible to collect name of method, line, file etc in which exception
occurred? I have heard its possible using System.Diagnostics.StackTrace but
not sure how.

Many Thanks

Regards

Author
23 Mar 2010 10:07 AM
Onur_Güzel
On Mar 23, 11:46 am, "John" <i...@nospam.infovis.co.uk> wrote:
> Hi
>
> Is it possible to collect name of method, line, file etc in which exception
> occurred? I have heard its possible using System.Diagnostics.StackTrace but
> not sure how.
>
> Many Thanks
>
> Regards

Handle exception in a try-catch block and call stacktrace property as
in the example:

/////////////////////////////////////

Try
            Dim a As Integer = 0
            Dim b As Integer = 0
            Dim c As Integer = 0

            a = a \ c
Catch ex As Exception
' View the line, file and method that exception was occured at.
MsgBox(ex.StackTrace)
End Try

/////////////////////////////////////


HTH,

Onur GÜZEL
Author
23 Mar 2010 11:17 AM
Herfried K. Wagner [MVP]
Am 23.03.2010 10:46, schrieb John:
> Is it possible to collect name of method, line, file etc in which exception
> occurred? I have heard its possible using System.Diagnostics.StackTrace but
> not sure how.

\\\
Dim CurrentStack As New StackTrace()
MsgBox("Current method:  " & CurrentStack.GetFrame(0).GetMethod().Name)
MsgBox("Calling method:  " & CurrentStack.GetFrame(1).GetMethod().Name)
MsgBox("Line number:  " &
CStr(CurrentStack.GetFrame(0).GetFileLineNumber()))
///

--
  M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
  V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>