10

The perfect StackOverflow question has finally come....

How do I catch a StackOverflow exception!

It seems in .NET Core the StackOverflowException isn't available:

enter image description here

And if I run this code:

using System;

namespace PlayGround.Core.Console
{
    public class Program
    {
        public static void Main(string[] args)
        {
            try
            {
                DoSomething();
            }
            catch(Exception e)
            {
                System.Console.WriteLine("Bugger");
            }

        }

        private static void DoSomething()
        {
            DoSomething();
        }
    }
}

I get this:

enter image description here

You can see my exception handler didn't run. So how do I go about catching this exception in .NET Core?

EDIT September 15th, 2017: In .NET Core 2.0 there is now a StackOverflowException class, but it still doesn't actually catch a stackoverflow exception.

7

1 Answer 1

2

Since CLR 2.0 you cant catch a StackOverflowException. Unless you were the one to throw it it.

https://blogs.msdn.microsoft.com/jaredpar/2008/10/22/when-can-you-catch-a-stackoverflowexception/

Sign up to request clarification or add additional context in comments.

2 Comments

The Gods Must Be Crazy
It's a pity so you can't know where in your code the exception happened. In the case of a big application, it's a problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.