Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
754 views
in Technique[技术] by (71.8m points)

exception - How can I configure call depth in PowerShell?

I was just trying things in PowerShell and got an error about call depth being set to 1000 in some test recursive function. I looked on the Internet for some information and found that this is due to error handling in PowerShell (if I got it right):

The recursion depth limit is fixed in version 1. Deep recursion was causing problems in 64-bit mode because of the way exceptions were being processed. It was causing cascading out-of-memory errors. The net result was that we hard-limited the recursion depth on all platforms to help ensure that scripts would be portable to all platforms. - Bruce Payette, co-designer of PowerShell

I found it here.

Also I found this exception page on MSDN that states this limit is configurable (but I didn't find anything about how to do this) - see remarks section here.

How can this limit be set?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  • In PowerShell V1 the maximum call depth is 100:

Using .NET Reflector, we can see in this snippet from the System.Management.ExecutionContext class code,

internal int IncrementScopeDepth()
{
    using (IDisposable disposable = tracer.TraceMethod("{0}", new object[] { this.scopeDepth }))
    {
        if (this.CurrentPipelineStopping)
        {
            throw new PipelineStoppedException();
        }
        this.scopeDepth++;
        if (this.scopeDepth > 100)
        {
            ScriptCallDepthException exceptionRecord = new
            ScriptCallDepthException(this.scopeDepth, 100);
            tracer.TraceException(exceptionRecord);
            throw exceptionRecord;
        }
        return this.scopeDepth;
    }
}

that it is not possible to modify the hardcoded 100.

  • In PowerShell V2 the maximum call depth is 1000

Again when looking at the code, there doesn't seem to be a way around the default maximum call depth.

  • In PowerShell V3 (CTP) there doesn't seem to be a maximum call depth (unless you run out of resources of course). This behaviour has been described as a bug on connect, so it might change in the final version.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...