Discussion:
Find the line number of an exception.
Brady Kelly
2008-08-01 11:03:50 UTC
Permalink
Is there a way to determine the line number that an exception is thrown on
other than parsing the StackTrace exception property? Why can't the
Exception class expose a real StackTrace?



Brady Kelly
Software Developer

Chase Software
Office: 011 287 1820
Cell: 078 331 3781
Fax: 086 691 5653

<http://www.chasesoftware.co.za/> www.ChaseSoftware.co.za

cid:***@01C872FF.BDFE3680
Cert_Partner_rgb_6_WithLogo.jpg

Chase Software is a certified ISV/Software Solutions Partner delivering
management solutions to advertising agencies & allied industries

Note: The contents of this email and any attachments are confidential, may
be privileged and may only be read and used by the named recipient(s). If
you have received it in error please contact the sender immediately and
delete the e-mail.




===================================
This list is hosted by DevelopMentor� http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
John Warner
2008-08-01 12:21:18 UTC
Permalink
Likely a function of the fact .NET supports any number of languages. What
constitutes a line?

John Warner
-----Original Message-----
From: Discussion of advanced .NET topics.
Sent: Friday, August 01, 2008 7:04 AM
Subject: [ADVANCED-DOTNET] Find the line number of an exception.
Is there a way to determine the line number that an exception
is thrown on other than parsing the StackTrace exception
property? Why can't the Exception class expose a real StackTrace?
Brady Kelly
Software Developer
Chase Software
Office: 011 287 1820
Cell: 078 331 3781
Fax: 086 691 5653
<http://www.chasesoftware.co.za/> www.ChaseSoftware.co.za
Cert_Partner_rgb_6_WithLogo.jpg
Chase Software is a certified ISV/Software Solutions Partner
delivering management solutions to advertising agencies &
allied industries
Note: The contents of this email and any attachments are
confidential, may be privileged and may only be read and used
by the named recipient(s). If you have received it in error
please contact the sender immediately and delete the e-mail.
===================================
This list is hosted by DevelopMentorR http://www.develop.com
View archives and manage your subscription(s) at
http://discuss.develop.com
===================================
This list is hosted by DevelopMentor� http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Brady Kelly
2008-08-01 13:16:39 UTC
Permalink
Post by John Warner
Likely a function of the fact .NET supports any number of languages. What
constitutes a line?
John Warner
It's actually quite easy, in C# at least:

private static void ShowDebugInfo(Exception ex)
{
StackTrace exTrace = new StackTrace(ex, true);
Console.WriteLine(exTrace.GetFrame(0).GetMethod().Name);

Console.WriteLine(exTrace.GetFrame(0).GetFileLineNumber().ToString());
}

===================================
This list is hosted by DevelopMentor� http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
John Warner
2008-08-01 13:24:56 UTC
Permalink
I'd never seen that was a property, thanks.

John Warner
-----Original Message-----
From: Discussion of advanced .NET topics.
Sent: Friday, August 01, 2008 9:17 AM
Subject: Re: [ADVANCED-DOTNET] Find the line number of an exception.
Post by John Warner
Likely a function of the fact .NET supports any number of
languages.
Post by John Warner
What constitutes a line?
John Warner
private static void ShowDebugInfo(Exception ex)
{
StackTrace exTrace = new StackTrace(ex, true);
Console.WriteLine(exTrace.GetFrame(0).GetMethod().Name);
Console.WriteLine(exTrace.GetFrame(0).GetFileLineNumber().ToString());
}
===================================
This list is hosted by DevelopMentorR http://www.develop.com
View archives and manage your subscription(s) at
http://discuss.develop.com
===================================
This list is hosted by DevelopMentor� http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Brady Kelly
2008-08-01 14:24:25 UTC
Permalink
Post by John Warner
I'd never seen that was a property, thanks.
John Warner
I'm curious as to how the StackTrace is constructed based on the exception.
On the face of it, it appears it would have to parse the StackTrace property
of the exception, or have 'insider' access to that information. The former
is very clumsy.

===================================
This list is hosted by DevelopMentor� http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Greg Rothlander
2008-08-06 17:08:16 UTC
Permalink
I'm moving some code from an old language and rewriting it in .Net. I have
come across an odd situation where I will have to go in and rework a great
deal of code and I'm looking for a less complex and time consuming solution.
Here's the basic idea...

In the old language they are using a string constant such as:

Const c1FullAddr as String = "1 Colonial Drive Parma Ohio 21321"

Then they follow that up with another constant:

Const c1FaLen as String = c2FullAddr.length

In VB.Net you cannot do this. Is there a way to do this without having to
count the length of c1FullAddr and hard coding the value or without changing
it from a constant? I can ceratianly go through and count the lengths and
hardcode the values, or I could do through and change all of the "const" to
"dim" and it would work, but it would like to keep it as a constant in the
new version as well, if I can.

Of course the most obvious solution would be to simply make it a string
variable and not a string constant. I will do that if I need to, but I
thought there might be a trick here to get this to work as a constant, but I
certainly cannot see any way to make it work as a constant.

I think the real issue here is that if it is defined at runtime and not
design time, then it's not really a constant. So maybe there is no way to
do this in VB.Net, as a constant and just simply changing it to a string
variable is the correct approach.

Any thoughts or suggestions would be very much appreciated.

Best regards,
Jon

===================================
This list is hosted by DevelopMentor� http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
John Warner
2008-08-06 17:21:40 UTC
Permalink
Go with the string, what the old code is doing is reserving an array and
then populating it later. VB and most modern languages I'm aware of won't
allow you to do this with a constant. Save the headaches of work arounds
and go with your gut, convert to DIM.

John Warner
-----Original Message-----
From: Discussion of advanced .NET topics. [mailto:ADVANCED-
Sent: Wednesday, August 06, 2008 1:08 PM
Subject: [ADVANCED-DOTNET] Setting a Constant Value in VB.Net
I'm moving some code from an old language and rewriting it in .Net. I
have
come across an odd situation where I will have to go in and rework a
great
deal of code and I'm looking for a less complex and time consuming
solution.
Here's the basic idea...
Const c1FullAddr as String = "1 Colonial Drive Parma Ohio
21321"
Const c1FaLen as String = c2FullAddr.length
In VB.Net you cannot do this. Is there a way to do this without having
to
count the length of c1FullAddr and hard coding the value or without
changing
it from a constant? I can ceratianly go through and count the lengths
and
hardcode the values, or I could do through and change all of the "const"
to
"dim" and it would work, but it would like to keep it as a constant in
the
new version as well, if I can.
Of course the most obvious solution would be to simply make it a string
variable and not a string constant. I will do that if I need to, but I
thought there might be a trick here to get this to work as a constant,
but I
certainly cannot see any way to make it work as a constant.
I think the real issue here is that if it is defined at runtime and not
design time, then it's not really a constant. So maybe there is no way
to
do this in VB.Net, as a constant and just simply changing it to a string
variable is the correct approach.
Any thoughts or suggestions would be very much appreciated.
Best regards,
Jon
===================================
This list is hosted by DevelopMentor. http://www.develop.com
View archives and manage your subscription(s) at
http://discuss.develop.com

===================================
This list is hosted by DevelopMentor� http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Stuart Dunkeld
2008-08-06 23:49:22 UTC
Permalink
Post by Greg Rothlander
Const c1FullAddr as String = "1 Colonial Drive Parma Ohio 21321"
Const c1FaLen as String = c2FullAddr.length
In VB.Net you cannot do this. Is there a way to do this without having to
count the length of c1FullAddr and hard coding the value or without changing
it from a constant?
How about:

Const c1FaLen = Convert.ToString(c1FullAddr).Length ?

On Wed, Aug 6, 2008 at 6:25 PM, Peter Ritchie
Post by Greg Rothlander
As you've guessed, there's no compile-time ability to get the length of a
string (or to execute any member of any const at compile-time).
Can I ask in the interest of increasing my understanding - where does
'compile time' come into it?

-- stuart

===================================
This list is hosted by DevelopMentor® http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
John Brett
2008-08-07 07:57:00 UTC
Permalink
Post by Stuart Dunkeld
Const c1FaLen = Convert.ToString(c1FullAddr).Length ?
Because it doesn't work?

c1FullAddr already is a string; there's no need to convert it to a
string to call its Length property.
It doesn't help because you can't call methods/properties to set the
value of a Const.
Post by Stuart Dunkeld
Can I ask in the interest of increasing my understanding - where does
'compile time' come into it?
It is the job of the compiler to work out the value of Consts, and it
(generally) cannot execute methods/properties of objects during the
compilation phase.

Peter's solution of using ReadOnly and John's(/your) solution of Dim
both solve the problem as calculating the value is deferred until the
value is first referenced (at run-time), when the run-time environment
can call methods/properties of other objects.

John

===================================
This list is hosted by DevelopMentor® http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Patrick Steele
2008-08-01 17:53:28 UTC
Permalink
My guess is that providing a line number would assume you've compiled
in debug mode and have the PDB file available. When you compile in
release mode, some optimizations are done and I don't think you can
clearly map a set of IL code directly to a line in the source file.

And once the method is JIT'd, then you've just got raw machine
language executing. I really doubt there's a way to map machine
instructions to a line# in a C#/VB.NET/whatever source file.
Post by Brady Kelly
Is there a way to determine the line number that an exception is thrown on
other than parsing the StackTrace exception property? Why can't the
Exception class expose a real StackTrace?
===================================
This list is hosted by DevelopMentor® http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Peter Ritchie
2008-08-06 17:25:23 UTC
Permalink
As you've guessed, there's no compile-time ability to get the length of a
string (or to execute any member of any const at compile-time).

ReadOnly members are the way to get around this problem. For example:

Public Shared ReadOnly c1FullAddr As String = "1 Colonial Drive Parma
Ohio 21321"
Public Shared ReadOnly c1FaLen As Integer = Program.c1FullAddr.Length

Personally, I think simply using c1FullAddr.Length instead of c1FaLen is
much more clear. Moving code from one language to another often involves
much more than translation; it often means re-writing the code to fit the
paradigm and colloquialisms of the destination language.
Post by Greg Rothlander
I'm moving some code from an old language and rewriting it in .Net. I
have
Post by Greg Rothlander
come across an odd situation where I will have to go in and rework a great
deal of code and I'm looking for a less complex and time consuming
solution.
Post by Greg Rothlander
Here's the basic idea...
Const c1FullAddr as String = "1 Colonial Drive Parma Ohio 21321"
Const c1FaLen as String = c2FullAddr.length
In VB.Net you cannot do this. Is there a way to do this without having to
count the length of c1FullAddr and hard coding the value or without
changing
Post by Greg Rothlander
it from a constant? I can ceratianly go through and count the lengths and
hardcode the values, or I could do through and change all of the "const"
to
Post by Greg Rothlander
"dim" and it would work, but it would like to keep it as a constant in the
new version as well, if I can.
Of course the most obvious solution would be to simply make it a string
variable and not a string constant. I will do that if I need to, but I
thought there might be a trick here to get this to work as a constant,
but I
Post by Greg Rothlander
certainly cannot see any way to make it work as a constant.
I think the real issue here is that if it is defined at runtime and not
design time, then it's not really a constant. So maybe there is no way to
do this in VB.Net, as a constant and just simply changing it to a string
variable is the correct approach.
Any thoughts or suggestions would be very much appreciated.
===================================
This list is hosted by DevelopMentor® http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Brady Kelly
2008-08-07 10:53:14 UTC
Permalink
Post by Peter Ritchie
Personally, I think simply using c1FullAddr.Length instead of c1FaLen
is much more clear.
You are not alone. I see no reason at all, in the modern world, to reserve
the length of a string as a const.

===================================
This list is hosted by DevelopMentor� http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Per Bolmstedt
2008-08-07 11:11:59 UTC
Permalink
Post by Greg Rothlander
Const c1FullAddr as String = "1 Colonial Drive Parma Ohio 21321"
Const c1FaLen as String = c2FullAddr.length
Since you're converting an old application, you might also want to be
careful so that no assumptions along the lines of "c1FaLen * 8 bits of
storage are needed for the address" are made somewhere. Usually when people
store the length of a string explicitly, it's, in my experience, used for
that or a similar purpose (writing records, allocating space etc). c1FaLen
will only tell you how many letters/characters/glyphs c1FullAddr contains
(the usefulness of which I don't really perceive).

===================================
This list is hosted by DevelopMentor® http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Peter Ritchie
2008-08-07 14:02:20 UTC
Permalink
Post by Stuart Dunkeld
Const c1FaLen = Convert.ToString(c1FullAddr).Length ?
As others have pointed out the compiler can't compile this, and it's
simply another way of writing the orginal code that didn't compile.
Post by Stuart Dunkeld
Can I ask in the interest of increasing my understanding - where does
'compile time' come into it?
There's code that's evaluated when it is compiled and there's code that's
evaluated when it's run. The compiler can evaluate constant expressions.
The compiler doesn't run code, it merely compiles it. When assigning to a
const member, the value being assigned is evaluated when compiled and
baked-in. Code that calls methods (or otherwise access non-const values)
cannot be executed at compile-time (because they are not constant
expressions) and thus can't be assigned to a const value.

Could the compiler simply translate "text".Length into 4? If it knew
what "Length" did, it could; but it currently doesn't know what Length
does. It's currently ignorant of what properties and methods do, it
simply knows what they are and how to emit IL that executes them at run-
time.

===================================
This list is hosted by DevelopMentor® http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Loading...