Tommy Sin

Tommy Sin

  • NA
  • 19
  • 104.4k

Why doesnt StackOverflowException get caught here?

Mar 14 2011 3:08 PM
I am wondering why the StackOverflowException is not caught while running the following program.

The program is just terminated due to StackOverflowException, why doesnt it catch the exception before it terminates?

Could anyone please explain? Thanks.




class program
{
        private static int val = 0;
        static void Main(string[] args)
        {
                Recursive();
        }

        static void Recursive()
        {
                try
                {
                        Console.WriteLine(val);
                        val++;
                        Recursive();
                }
                catch(StackOverflowException e)
                {
                        Console.WriteLine(e.Message);
                        return;
                }
        }
}

Answers (1)