Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can Environment.StackTrace throw ArgumentOutOfRangeException?

Tags:

c#

.net

According to MSDN, Environment.StackTrace can throw ArgumentOutOfRangeException but I don't understand how this is possible.

Environment.cs StackTrace (source)

public static String StackTrace {
    [System.Security.SecuritySafeCritical]
    get {
        Contract.Ensures(Contract.Result<String>() != null);

        new EnvironmentPermission(PermissionState.Unrestricted).Demand();
        return GetStackTrace(null, true);
    }
}

Calls GetStackTrace(Exception, bool) where Exception is null.

Environment.cs GetStackTrace(Exception, bool) (source) (comments removed, they are irrelevant)

internal static String GetStackTrace(Exception e, bool needFileInfo)
{
    StackTrace st;
    if (e == null)
        st = new StackTrace(needFileInfo);
    else
        st = new StackTrace(e, needFileInfo);

    return st.ToString( System.Diagnostics.StackTrace.TraceFormat.Normal );
}

The above method has the potential to call two constructors of StackTrace, StackTrace(bool) and StackTrace(Exception, bool).

We know from the first call that if this method is reached via the Environment.StackTrace call then StackTrace(bool) is guaranteed to be called.

But StackTrace(bool) doesn't throw any exceptions according to MSDN. The other possible constructor call, StackTrace(Exception, bool) (MSDN) does throw an exception, but it's ArgumentNullException not ArgumentOutOfRangeException. I don't see any other method calls made in the code that would throw ArgumentOutOfRangeException.

So what am I missing? Is it actually possible for Environment.StackTrace to throw an exception and if so how?

like image 297
Adam Avatar asked Nov 13 '15 18:11

Adam


1 Answers

Retrieving the property value in fact shouldn't throw an exception. We've removed the exception information from the documentation for the Environment.StackTrace property.

like image 56
Ron Petrusha - MSFT Avatar answered Nov 15 '22 04:11

Ron Petrusha - MSFT