473,385 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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



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
5 22302
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
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
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


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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Timo | last post by:
For some reason Python 2.3.5 reports the error in the following program correctly: File "C:\Temp\problem.py", line 7 SyntaxError: unknown decode error ...whereas Python 2.4.1 reports an...
11
by: Ken Varn | last post by:
I want to be able to determine my current line, file, and function in my C# application. I know that C++ has the __LINE__, __FUNCTION__, and __FILE___ macros for getting this, but I cannot find a...
9
by: kangaroo | last post by:
Hi guys, when we run a stored proc and it results into an unhandled error, the error message returned by db2 udb does not contain a line number that caused an error. This makes it pretty...
6
by: Phil Jollans | last post by:
Hi, I have an error dialog which shows the stack trace, usually without line numbers in the release version. To try to diagnose a particular problem, I have provided a user with a debug version...
5
by: Mark Kamoski | last post by:
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...
2
by: johnivey | last post by:
I have a large query that I am trying to debug in query analyzer. However, the errors I get have no line number or reference to where they are failing. How can I find out what line in the query is...
6
by: Rotsey | last post by:
Hi, Am trying to be able to the line number of an exception. The stack trace is not showing it. rotsey
7
by: Daniel | last post by:
hi guys i have never understood this so i am hoping someone can help. I have had errors in my app before where on closing it down i get a NullReferenceException error thrown BUT no line number...
3
by: Dan Holmes | last post by:
Server stack trace: at IVS.Framework.ControlNumberService.InstallComponent(Identity id, ControlNumberInfo ctrlNumber) in...
4
by: Jeff Jarrell | last post by:
I have a block of code that during development is prone to casting errors. It is mostly a DataReader type thing. It looks something like this. _prtPNID = myDLReader.GetString("prtPNID")...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.