473,387 Members | 1,502 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,387 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 17 '05 #1
5 1936
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 17 '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 17 '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 17 '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 17 '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 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bob Bamberg | last post by:
Hello All, I have been trying without luck to get some information on debugging the Runtime Error R6025 - Pure Virtual Function Call. I am working in C++ and have only one class that is derived...
9
by: fabio de francesco | last post by:
Hi, I would like to know why objects that are thrown to be caught are copy-constructed more than once. I have seen this behaviour after running the following simple program that is an...
1
by: Eric Gibson | last post by:
Hey, I want to be able to get a sensible exception message that I can actually use to fix my code using asp.net... I was able to set up the Application_Error event, and email exceptions to...
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...
4
by: chopsnsauce | last post by:
Here is the problem. I'm opening a form that Throw's an error in the Load event and the method thant opens the form has a try..Catch to Catch the error that is thrown in the load event. This...
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...
1
by: SushmaLabh | last post by:
I am usinng J2EE 1.4 SDK.I got this error.Pls Hepl me. org.apache.jasper.JasperException: Unable to compile class for JSP Generated servlet error: ...
6
by: Cruithne3753 | last post by:
I'm trying to create a Windows app with a clickable index of images within a local folder. Within a loop I've got:- PictureBox pb = new PictureBox(); pb.Image = Image.FromFile(file); .......
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.