How can Environment.StackTrace throw an ArgumentOutOfRangeException?

According to MSDN , Environment.StackTrace can raise an 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 are deleted, 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 can call two StackTrace constructors, StackTrace(bool) and StackTrace(Exception, bool) .

We know from the first call that if this method is achieved by calling Environment.StackTrace , then StackTrace(bool) guaranteed to be called.

But StackTrace(bool) does not throw any exceptions according to MSDN . Another possible call to the StackTrace(Exception, bool) constructor StackTrace(Exception, bool) ( MSDN ) throws an exception, but it is ArgumentNullException not ArgumentOutOfRangeException . I do not see any other method calls made in the code that would throw an ArgumentOutOfRangeException .

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

+5
source share
2 answers

Retrieving a property value should not actually throw an exception. We have removed the exception information from the documentation for the Environment.StackTrace property.

+3
source

Reading Source Code I agree that an ArgumentOutOfRangeException should never be thrown into the code path from Environment.StackTrace.

This is not the first time that MSDN needs to be fixed. Do you have to click "Any suggestions"? link in the upper right corner. After the response, β€œWas the page helpful? Yes / No,” they will ask for any other feedback.

+2
source

Source: https://habr.com/ru/post/1235907/


All Articles