Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catching a StackOverflowException #8947

Closed
joshcomley opened this issue Sep 15, 2017 · 3 comments
Closed

Catching a StackOverflowException #8947

joshcomley opened this issue Sep 15, 2017 · 3 comments

Comments

@joshcomley
Copy link

If I have this simple app:

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();
		}
	}
}

The exception handler itself is never called, instead the app crashes and I get this error:

7mwfr

The same happens if I use the StackOverflowException in the catch instead of just Exception.

Is there anyway to catch a StackOverflow exception and not have the app crash?

@sharwell
Copy link
Member

sharwell commented Sep 15, 2017

To date I have not found a way to handle a StackOverflowException within a .NET process, outside of the case where you use the hosting API to launch the CLR in a non-standard mode which allows these exceptions to be caught. I've tried everything I could think of, including routing calls through an intermediate unmanaged C++ library using unmanaged exception handlers to try and prevent the CLR from seeing the exception. I would be delighted if someone found a way to "intercept" this exception on a thread.

📝 The expectation is a StackOverflowException cannot be caught in a managed application. The CLR goes to great lengths to remove your ability to handle it. If there exists a way to catch it, I would not expect it to be reliable since it would indicate a "hole" in the intended feature set and a later update could easily remove the ability.

@jkotas
Copy link
Member

jkotas commented Sep 15, 2017

Right, StackOverflowException is non cachable.

Related to #4113

@jkotas jkotas closed this as completed Sep 15, 2017
@masums
Copy link

masums commented Apr 28, 2018

But there should have any way to prevent app crash for StackOverflowException because for that full web app crashes. Which is an unexpected behavior.

@msftgits msftgits transferred this issue from dotnet/coreclr Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants