2

From my practice, HandleProcessCorruptedStateExceptions not works for StackOverflow exception but OK for AccessViolationException.

AccessViolationException from this post which is caught:

public class Test
{
    public static void Main(string[] args)
    {
        Wtf();
    }

    [SecurityCritical]
    [HandleProcessCorruptedStateExceptions]
    private static void Wtf()
    {
        try
        {
            IntPtr ptr = new IntPtr(1000);
            Marshal.StructureToPtr(1000, ptr, true);
        }
        catch (Exception e)
        {
            Environment.Exit(1);
        }
    }
}

StackOverflow exception written by myself, which catch nothing.

public class Test
{
    public static void Main(string[] args)
    {
        Wtf();
    }

    [SecurityCritical]
    [HandleProcessCorruptedStateExceptions]
    private static void Wtf()
    {
        try
        {
            F();
        }
        catch (Exception e)
        {
            Environment.Exit(1);
        }
    }

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

Could someone explain this? Thanks!

Junle Li
  • 1,035
  • 1
  • 12
  • 18

1 Answers1

1

StackOverflowException is not considered a Corrupted State Exception by .NET so it can't be caught in this fashion. See How do I prevent and/or handle a StackOverflowException? for some other answers on how to do it instead.

Community
  • 1
  • 1
MatthewG
  • 8,583
  • 2
  • 25
  • 27
  • I know that is hard to catch Stackoverflow. So I am consodering start a child process and monitor it. But in practice, I don't know which information indicate a process is throwing Stackoverflow. Any ideas? – Junle Li Feb 04 '15 at 02:29