Re: Sanity Check: Custom Exception Approach

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Dave (noSpamdlevineNNTP2_at_wi.rr.com)
Date: 03/25/04


Date: Thu, 25 Mar 2004 05:22:53 -0600


>
> One surprising discovery for me is that when I pass the various Exception
> objects to an error logging routine (which writes details out to an XML
> file), the StackTrace associated with the exception (e.g.,
> SqlException.StackTrace) does not include the entire call stack (at least
it
> does not show all the modules/methods invoked from the start of the
> application and leading up to the error). However, at the same time - the
> same point in execution time - the Environment.StackTrace does contain the
> entire call stack.

This is the expected behavior. The exception object captures the stack from
the point where the exception occurred to where the exception was caught -
this is not the entire stack. Also, if you use the StackTrace property
directly you will get the stack trace for only that exception object, but
not for any innerExceptions. If you want a dump for all the chained
exceptions then user the exception.ToString() method (or run the chain
yourself).

If you call Environment.StackTrace in the catch handler you will see a
different stack due to the nature of how Structured Exception Handling (SEH)
works in .net. This uses a 2 pass mechanism when an exception occurs. The
1st pass locates a catch block for the exception, and the 2nd pass unwinds
stack frames, executing finally and fault blocks, up to the frame of the
catch handler. By the time the code in the catch handler executes the stack
has already been unwound from the point of the exception to the catch
handler. This means that if you capture the stack at that point you will
not see the frames that actually threw the exception - those frames have
been unwound (finally blocks executed) and popped off the stack.

> Put another way, when the program is executing the
> logging routine (in a static helper class), the two call stacks are not
> equal. They are not equal in entries leading up to the point of the
> exception, and of course only the Environment.StackTrace includes the call
> to the error logging routine itself (but that's not a surprise).
> Consequently in order to log everything I want to know about the
StackTrace,
> I am getting different pieces of information from the two different Stack
> Traces. I'd be interested in knowing what the difference is because I
> thought there was just one stack - and if there is, then any stacktrace
> should show what's on it.
>

Basically, the exception object preserves the stack frames for reporting
purposes and then pops the stack; Environment.StackTrace reports on the
current stack but knows nothing about what happened before that point.

Dave



Relevant Pages

  • Re: Try Finally...
    ... stack frames on the stack that control program flow into and out of ... so also are guard frames allocated on the stack. ... Exception bubbling uses the chain of guard frames, ...
    (comp.lang.pascal.delphi.misc)
  • Re: Try Finally...
    ... It is governed by guard frames placed in the program's stack. ... >exception, the exception is not created in Delphi (or whatever language we are ...
    (comp.lang.pascal.delphi.misc)
  • Re: x86 exception handling and stack demand
    ... When an exception occurs that will be passed down to user mode as an SEH exception, the kernel arranges for control to return to user mode at a special function in NTDLL, with several parameters on the stack containing information about the exception. ... In XP and later, the system stores a pointer to the initial stack allocation block in the TEB that is used by the kernel to decommit the stack via NtFreeVirtualMemory when the thread is terminated in a non-graceful fashion, closing this leak. ...
    (microsoft.public.win32.programmer.kernel)
  • [PATCH] x86: style fascism for xen assemblies
    ... * a view to being able to inline as much as possible. ... push %eax ... * This is run where a normal iret would be run, with the same stack setup: ... In order to deliver the nested exception properly, ...
    (Linux-Kernel)
  • RE: System.AccessViolationException in .NET 2.0 application
    ... Based on the call stack, there is an AV exception in the ... Microsoft Online Community Support ...
    (microsoft.public.dotnet.general)