how to get the line number where error was thrown and/or caught

  • Thread starter Thread starter Mark Kamoski
  • Start date Start date
M

Mark Kamoski

Hi Everyone--

How can one get the line number of where an error was thrown and/or caught?

For example, note the following, for use at any given point in a piece of
code:

....to get the current Assembly's name, one can use this...

System.Reflection.Assembly.GetExecutingAssembly().GetName().Name

....and, to get the current class's name, one can use this...

System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name

....and, to get the current method's name, one can use this...

System.Reflection.MethodBase.GetCurrentMethod().Name

....BUT...

....to get the line number of where an error was thrown and/or caught one
can use???


WHAT?

Any ideas?

(Note that, in short, I would like to get the line number the same way as
when DotNet reports an unhandled exception, where it gives the line number
of the break.)

Please advise.

Thank you.

--Mark
 
Hello,

Mark Kamoski said:
How can one get the line number of where an error was thrown
and/or caught?

You can set error numbers by hand:

\\\
Public Sub Bla()
1: Dim i As Integer
2: i = 2
3 Dim s As String = "Hello World"
End Sub
///

When using 'On Error...' error handlers, you can determine the line number
by reading the 'Erl' property of the 'Err' object.

- or -

StackTrace:

http://groups.google.de/groups?selm=#[email protected]
http://groups.google.de/groups?selm=OHh5458mCHA.1608@TK2MSFTNGP10
 
Hi Mark,

The following will give you the line number of your code (in the
source file):

Dim CurrentStack As System.Diagnostics.StackTrace
MsgBox (CurrentStack.GetFrame(0).GetFileLineNumber)

In case you're interested, you can find out about the routine that you're
in, as well as all its callers.

Public Function MeAndMyCaller As String
Dim CurrentStack As New System.Diagnostics.StackTrace
Dim Myself As String = CurrentStack.GetFrame(0).GetMethod.Name
Dim MyCaller As String = CurrentStack.GetFrame(1).GetMethod.Name
Return "In " & Myself & vbCrLf & "Called by " & MyCaller
End Function

This can be very handy if you want a generalised error routine because it
can get the name of the caller (which would be where the error occurred).

Regards,
Fergus
MVP [Windows Start button, Shutdown dialogue]
 
Hello,

Fergus Cooney said:
The following will give you the line number of your code (in the
source file):

Dim CurrentStack As System.Diagnostics.StackTrace
MsgBox (CurrentStack.GetFrame(0).GetFileLineNumber)

Notice that this will require the PDB files and should not be used with
builds deployed to end users.
 
Hi Fergus (and everyone)--

Thank you for your reply.

That certainly IS handy and I'll use it.

However, here are some follow-up questions.


(1). Is it possible to grab this line number value in one line of code,
with a static call?

For example, to get the currently executing method name at a given point in
code, one can make this call...

System.Reflection.MethodBase.GetCurrentMethod().Name

....and out pops the method name.

Is there something like that that works to grab the current line number?

(The Reflection objects are not at all clear to me, so I am having trouble
finding it there.)


(2). Also, since it was pointed out that PDBs are required and that one
must be in Debug Mode for the call you show below to work, do you know of a
handy way to have the code check to see if it is executing in Debug Mode or
otherwise?



Please advise.

Thank you so much.

--Mark



Hi Mark,

The following will give you the line number of your code (in the
source file):

Dim CurrentStack As System.Diagnostics.StackTrace
MsgBox (CurrentStack.GetFrame(0).GetFileLineNumber)

In case you're interested, you can find out about the routine that
you're
in, as well as all its callers.

Public Function MeAndMyCaller As String
Dim CurrentStack As New System.Diagnostics.StackTrace
Dim Myself As String = CurrentStack.GetFrame(0).GetMethod.Name
Dim MyCaller As String =
CurrentStack.GetFrame(1).GetMethod.Name
Return "In " & Myself & vbCrLf & "Called by " & MyCaller
End Function

This can be very handy if you want a generalised error routine because
it
can get the name of the caller (which would be where the error occurred).

Regards,
Fergus
MVP [Windows Start button, Shutdown dialogue]
 
Hello,

Mark Kamoski said:
(2). Also, since it was pointed out that PDBs are required
and that one must be in Debug Mode for the call you show
below to work, do you know of a handy way to have the
code check to see if it is executing in Debug Mode or
otherwise?

You may want to experiment with:

\\\
#If Debug Then
Console.WriteLine("Debug mode.")
#Else
Console.WriteLine("Release mode.")
#End If
///

You can check if a debugger is attached by calling
'System.Diagnostics.Debugger.IsAttached'.
 
Back
Top