Groups | Blog | Home
all groups > dotnet general > december 2007 >

dotnet general : Exception and StackTrace .... more info ?? HOW?


Kristijan Marin
12/28/2007 7:39:10 PM
Hi,

Can please anyone tell me why do i get only the first method in which the
exception was thrown and not the line that triggered the exception ? I don't
know if I set something in VS2005 wrong or what the hell is wrong ....

I have a Windows Service in RELEASE mode , on VS2005 C# .....

So to summarize ... when the exception is thrown, I catch it and display the
stacktrace of the exception, but as I said above, only the calling method is
showen..... the exception is thrown in QueueService.queueTimer_Elapsed
method .....


Thanks a lot.
Kris

Example :

<here I would expect to see the line of exception or at least the name of
the method ....>
at ReportQueueService.QueueService.queueTimer_Elapsed(Object sender,
ElapsedEventArgs e)
at System.Timers.Timer.MyTimerCallback(Object state)
at System.Threading._TimerCallback.TimerCallback_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading._TimerCallback.PerformTimerCallback(Object state)

Peter Ritchie [C# MVP]
12/28/2007 9:24:00 PM
CallStacks don't have line numbers in them, only the method names.

When running code the runtime runs IL, not C#--so line numbers are
meaningless at that point.

--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#


[quoted text, click to view]
Jon Skeet [C# MVP]
12/30/2007 7:23:13 PM
[quoted text, click to view]

And unless inlining has occurred, this will usually be the case.

However, without a short but complete program demonstrating the
problem, it's hard to say why you're not seeing the behaviour you
expect.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Kristijan Marin
12/30/2007 7:32:53 PM
Hi,

I understand, but as I said, I would expect a little more form StackTrace
then just the Parant method .... cause if i have a method with 100 lines,
and exception occures in some iostream like "ReadLine" then I would expect
to see that Exception occured in "ReadLine" method

For instance:

public void MyMethod()
{
int i;
try
{
..
..
..
..
o.ReadLine(); ///exception here
..
..
..
}catch(Exception e)
{
WRITE STACK

}
}




..... I don't know if this was ever in C# like there is trace in JAVA where
you see exactly where it happened or not, but I think this was showen when I
was doing Desktop application ...

Thx.
Kris

[quoted text, click to view]

Jon Skeet [C# MVP]
12/31/2007 12:41:40 AM
[quoted text, click to view]

No, not really.

You *could* be rethrowing an exception using:

throw e;

instead of

throw;

[quoted text, click to view]

No, it won't be a VS setting. Try the following complete program, in
Debug mode (to avoid inlining).

using System;

class Test
{
static void Main()
{
try
{
Outer();
}
catch (Exception e)
{
Console.WriteLine (e);
}
}

static void Outer()
{
Inner();
}

static void Inner()
{
ThrowException();
}

static void ThrowException()
{
throw new Exception("Ouch!");
}
}

You should get a stack trace like this:

System.Exception: Ouch!
at Test.ThrowException() in c:\Users\Jon\Test\Test.cs:line 29
at Test.Inner() in c:\Users\Jon\Test\Test.cs:line 24
at Test.Outer() in c:\Users\Jon\Test\Test.cs:line 19
at Test.Main() in c:\Users\Jon\Test\Test.cs:line 9

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Kristijan Marin
12/31/2007 1:36:09 AM
Hi,

Ok now we understand each other ..... :) ....

So please tell me, could it be possible that I somewhere turned some VS or
Project property off or on and this
now gives the "not expected" stack result ?

I doubt that program example would somehow help, cause this is happening to
all Window Service
applications that i have :) so I think that it could be some VS setting
problem .... but don't know which ....

Thanks.
Kris

[quoted text, click to view]

Adam Benson
1/2/2008 12:04:30 PM
Does this help ?



public static string StackTraceToString(System.Diagnostics.StackTrace trace)

{

StackFrame sf;

System.Text.StringBuilder s = new System.Text.StringBuilder();

for (int i = 0; i < trace.FrameCount; i++)

{

sf = trace.GetFrame(i);

s = s.AppendFormat(" {0} in {1} (Line {2}, Column {3}){4}",

sf.GetMethod(),

sf.GetFileName(),

sf.GetFileLineNumber(),

sf.GetFileColumnNumber(),

Environment.NewLine );

}

return s.ToString();

}



What happens if you try a debug build rather than a release build?



Cheers,



Adam.

=======


[quoted text, click to view]

AddThis Social Bookmark Button