title: Gracefully handling corrupted state exceptions in .NET Core
body: <p>I have basically a duplicate <a href="https://stackoverflow.com/questions/39956163/gracefully-handling-corrupted-state-exceptions">question</a> but for .NET Core.</p>
  
  <p>I have Core console app:</p>
  
  <pre><code>class Program
  {
      static void DoSomeAccessViolation()
      {
          // if you have any questions about why this throws,
          // the answer is "42", of course
  
          var ptr = new IntPtr(42);
          Marshal.StructureToPtr(42, ptr, true);
      }
  
      [SecurityCritical]
      [HandleProcessCorruptedStateExceptions]
      static void Main(string[] args)
      {
          try
          {
              DoSomeAccessViolation();
          }
          catch (Exception ex)
          {
              Console.Error.WriteLine(ex);
          }
      }
  }
  </code></pre>
  
  <p>I've tried to add a Settings.setting file</p>
  
  <pre><code>&lt;SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="ConsoleApp2" GeneratedClassName="Settings1"&gt;
    &lt;Profiles /&gt;
    &lt;Settings&gt;
      &lt;Setting Name="legacyCorruptedStateExceptionsPolicy" Type="System.Boolean" Scope="Application"&gt;
        &lt;Value Profile="(Default)"&gt;True&lt;/Value&gt;
      &lt;/Setting&gt;
    &lt;/Settings&gt;
  &lt;/SettingsFile&gt;
  </code></pre>
  
  <p>and I've tried to set an environment variable according to <a href="https://github.com/dotnet/coreclr/blob/84efbc8e71fff8ccffdd637017036d2dc0f666df/Documentation/project-docs/clr-configuration-knobs.md" rel="nofollow noreferrer">docs</a>:</p>
  
  <pre><code>C:\&gt;set COMPlus_legacyCorruptedStateExceptionsPolicy=1
  C:\&gt;dotnet run
  </code></pre>
  
  <p>or </p>
  
  <pre><code>C:\&gt;set COMPlus_legacyCorruptedStateExceptionsPolicy=true
  C:\&gt;dotnet run
  </code></pre>
  
  <p>(on Windows). But nothing works, the app always crashes hard without printing the exception.</p>