dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.11k stars 4.7k forks source link

Error CS0246 referencing System.StackOverflowException and dotnet.exe crashes without an exception being caught #19263

Closed ericwj closed 4 years ago

ericwj commented 7 years ago

What should I do to reference System.StackOverFlowException?

I have use to catch StackOverflowException, since I am writing code that would rely on the compiler doing tail call optimization. However, trying to catch System.StackOverFlowException, I am presented with CS0246. I tried to explicitly include some packages, but that was probably futile in concept, since the class should be part of the Microsoft.NETCore.App dependency I am using (v1.0.1).

I'm sure it's possible as I'm seeing it being referenced in corefx/src/System.Runtime/tests/System/StackOverflowExceptionTests.cs. However, that repo is notoriously difficult to dissect - the test project for example doesn't have a project.json...

Yet, there is another issue, being that even if I write a simple test using Xunit and use this line:

Assert.ThrowsAny<Exception>(() => TailCall());

Whatson will kick in reporting that

dotnet has stopped working

and obviously, that is very unfortunate and useless.

Reproduce by running:

mkdir dotnetnew
code dotnetnew

Paste

using System;

namespace ConsoleApplication
{
    public class Program
    {
        public static int TailCall(int i) {
            if (i == int.MaxValue) return i;
            i++;
            return TailCall(i);
        }
        public static void Main(string[] args)
        {
            try {
                Console.WriteLine(TailCall(0));
            } catch (Exception ex) {
                Console.WriteLine($"{ex.GetType().Name}");
            }
        }
    }
}

This code works:

PS C:\dotnetnew> dotnet run -c Release
Project dotnetnew (.NETCoreApp,Version=v1.0) will be compiled because expected outputs are missing
Compiling dotnetnew for .NETCoreApp,Version=v1.0

Compilation succeeded.
    0 Warning(s)
    0 Error(s)

Time elapsed 00:00:01.1238355

2147483647

But not always:

dotnet run

dotnet has stopped working

Replace Exception with StackOverflowException.

dotnet build

\dotnetnew\Program.cs(11,27): error CS0246: The type or namespace name 'StackOverFlowException' could not be found (are you missing a using directive or an assembly reference?)

karelz commented 7 years ago

You seem to have bad casing in StackOverflowException in your error message (capital 'F' is incorrect). Could that be the reason? The tests seem to be able to reference it just fine.

ericwj commented 7 years ago

Except I didn't type it in the first place - I only did when Ctrl+Space wouldn't find anything. StackOverflowException

karelz commented 7 years ago

The type does not exist in .NET Core 1.0 nor 1.1 (about to be released). It will be part of our next release - 1.2 (currently scheduled for Spring 2017 - see milestones). The type was added into our dev/api branch in PR dotnet/corefx#10699.

ericwj commented 7 years ago

Oké then I can stop looking ;) Thanks!

masums commented 6 years ago

Is there have any way to detect System.StackOverflowException ( Which code segments are causing that? ) and prevent app crash at DotNet Core 2.0 or above?

karelz commented 6 years ago

Let's not highjack old bugs for related questions. If you need to debug it, get a dump and analyze the callstack - it will tell you more. There's also general info on the topic: https://stackoverflow.com/questions/1599219/c-sharp-catch-a-stack-overflow-exception