4

Sometimes I create an Exception instance without throwing it (for example to pass it directly to handler).

OnException(new AuthorizationException());

How to initialize its stack trace with the current location?

isanae
  • 3,253
  • 1
  • 22
  • 47
Poma
  • 8,174
  • 18
  • 82
  • 144
  • possible duplicate of [How to print the current Stack Trace in .NET without any exception?](http://stackoverflow.com/questions/531695/how-to-print-the-current-stack-trace-in-net-without-any-exception) – cbp Feb 10 '12 at 13:16
  • The question is about how to attach current stack trace to an exception without throwing it – Poma Feb 10 '12 at 13:20

3 Answers3

4

You can use Environment.StackTrace property or use StackTrace class:

var stack = new StackTrace();
var data = stack.<whatever you need from it>

But I just have to add: what you do is VERY bad conceptually.

Alexey Raga
  • 7,457
  • 1
  • 31
  • 40
  • 2
    Well that's because I'm doing a wrapper around badly designed library to make it a little nicer – Poma Feb 10 '12 at 13:18
  • 3
    Good answer. It would be even better if you explained *why* you consider *"very bad conceptually"* what the OP is doing. Creating an exception without throwing it is unusual, but not *that* far-fetched IMHO. – stakx - no longer contributing Aug 10 '12 at 15:25
3

You're actually asking two different questions (one in the title, the other at the end).

"How to capture the stack trace?"

Simply by querying the static System.Environment.StackTrace property or via new System.Diagnostics.StackTrace();.

Reading this property does not require you to construct an exception object at all, so perhaps it is all you need.

"How to initialize [an Exception object's] stack trace with current location?"

An exception object's StackTrace property is not initialized until you actually throw the exception object.

"The stack trace is created at the time the exception is thrown. This is in contrast to Java, where the stack trace is created during the construction of the exception object […]."The Common Language Infrastructure Annotated Standard, ch. 18, p. 301.

Since it is a read-only property, you cannot initialize it yourself — unless you derive your own exception class:

// don't do that:
class ExceptionWithPresetStackTrace : System.Exception
{
    public ExceptionWithPresetStackTrace(string stackTrace)
    {
        this.stackTrace = stackTrace;
    }

    public override string StackTrace
    {
        get
        {
            return stackTrace;
        }
    }
    readonly string stackTrace;
}

Combined with the answer to the first question, you could then do this:

OnException(new ExceptionWithPresetStackTrace(System.Environment.StackTrace));

However, this is generally a bad idea because it makes it possible to create exception objects that point the developer at any random location (via the StackTrace property), even ones where no error actually occurred. This is misleading and should be avoided.

stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
0

You can get the current stack trace as a string:

http://msdn.microsoft.com/en-us/library/system.environment.stacktrace.aspx

Alexander Zbinden
  • 2,315
  • 1
  • 17
  • 21