Exception and StackTrace .... more info ?? HOW?

K

Kristijan Marin

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)
 
P

Peter Ritchie [C# MVP]

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

Kristijan Marin

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
 
J

Jon Skeet [C# MVP]

Kristijan Marin said:
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

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

Kristijan Marin

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
 
J

Jon Skeet [C# MVP]

Kristijan Marin said:
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 ?

No, not really.

You *could* be rethrowing an exception using:

throw e;

instead of

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

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
 
A

Adam Benson

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.

=======
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top