1
Answer

Why doesnt StackOverflowException get caught here?

Tommy Sin

Tommy Sin

13y
2.4k
1
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)