By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,019 Members | 1,073 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,019 IT Pros & Developers. It's quick & easy.

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

P: n/a


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().De claringType.Name

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

System.Reflection.MethodBase.GetCurrentMethod().Na me

....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


Nov 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hello,

"Mark Kamoski" <mk******@yahoo.com> schrieb:
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=...TNGP11.phx.gbl
http://groups.google.de/groups?selm=...40TK2MSFTNGP10

--
Herfried K. Wagner
MVP VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #2

P: n/a
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]
Nov 20 '05 #3

P: n/a
Hello,

"Fergus Cooney" <fi******@tesco.net> schrieb:
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.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #4

P: n/a


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().Na me

....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

"Fergus Cooney" <fi******@tesco.net> wrote in message
news:Ou**************@TK2MSFTNGP12.phx.gbl...
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]

Nov 20 '05 #5

P: n/a
Hello,

"Mark Kamoski" <mk******@yahoo.com> schrieb:
(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'.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.