I tried to "measure" the depth of the stack. Why doesn't the following program 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 indicate a quote from MSDN, for example, "Starting with the .NET Framework version 2.0, a StackOverflowException cannot be caught by a try-catch block, and the corresponding process ends by default." Believe me, sometimes (when there is enough space for the stack), this can be considered, the following prints a certain 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); } }
c #
Prankster
source share