Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 189999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:13:09+00:00 2026-05-11T16:13:09+00:00

I was trying to measure stack depth. Why the following program doesn’t print anything?

  • 0

I was trying to “measure” stack depth. Why the following program doesn’t print anything?

class Program
{
    private static int Depth = 0;

    static void A(object o) 
    {
        Depth++;
        A(o);
    }

    static void B(object o, bool e)
    {
        Console.WriteLine(Depth);
    }

    static void Main(string[] args)
    {
        RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(A, B, null);
    }
}

Some answers simply include a quote from MSDN, like “Starting with the .NET Framework version 2.0, a StackOverflowException object cannot be caught by a try-catch block and the corresponding process is terminated by default.” Believe me, sometimes (when there is enough stack space) it can be cought, the following prints some number just fine:

class Program
{
    private static int depth = 0;

    static void A(object o)
    {
        depth++;
        if (Environment.StackTrace.Length > 8000)
            throw new StackOverflowException("Catch me if you can.");
        A(o);
    }

    static void B(object o, bool e)
    {
        Console.WriteLine(depth);
    }

    static void Main(string[] args)
    {
        RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(A, B, null);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T16:13:09+00:00Added an answer on May 11, 2026 at 4:13 pm

    If you want to catch it, load it into another process (that calls-back to yours via remoting) and lets the miscreant code execute there. The other process may terminate, and you could get a neat SOE popping out the end of the pipe on your side – without the adverse effects of the rather inconvenient exception.

    Note that a separate AppDomain in the same process won’t cut it.

    If you want to get the stack trace from an exception the following code will do you great justice:

        class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    Recurse(0);
                }
                catch (Exception ex)
                {
                    StackTrace st = new StackTrace(ex);
                    // Go wild.
                    Console.WriteLine(st.FrameCount);
                }
                Console.ReadLine();
            }
    
            static void Recurse(int counter)
            {
                if (counter >= 100)
                    throw new Exception();
                Recurse(++counter);
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 118k
  • Answers 118k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It it's aglobal variable, it will be a property of… May 11, 2026 at 11:30 pm
  • Editorial Team
    Editorial Team added an answer It's worth noting that in the first case they're only… May 11, 2026 at 11:30 pm
  • Editorial Team
    Editorial Team added an answer Update Looks like Apple made an IETF draft proposal, and… May 11, 2026 at 11:30 pm

Related Questions

I was trying to measure stack depth. Why the following program doesn't print anything?
I've noticed over time that Clojure users have nothing but massive enthusiasm for the
i've been trying to find a method in C# to measure the size of
I recently read the excellent article The Transactional Memory / Garbage Collection Analogy by
I come from a fairly strong OO background, the benefits of OOD & OOP

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.