title: How to test handling of AccessViolationException
body: <p>I need to write a test which verifies that my code can handle an AccessViolationException (or any other WIN32 Corrupted State Exception - CSE), which occours in an unsafe context, typically by calling a 3rd party lib.
  This should all be done using C# on .net 4.0.</p>
  
  <p>I found this related question <a href="https://stackoverflow.com/questions/3469368/how-to-handle-accessviolationexception">How to handle AccessViolationException</a> and this related article <a href="http://dotnetslackers.com/articles/net/All-about-Corrupted-State-Exceptions-in-NET4.aspx" rel="noreferrer">http://dotnetslackers.com/articles/net/All-about-Corrupted-State-Exceptions-in-NET4.aspx</a>, which explains how to catch these CSE's and their background.</p>
  
  <p>So i would like to provoke a WIN32 CSE in a test, to ensure correct handling in my application. Something like:</p>
  
  <p>Some example class to test:</p>
  
  <pre><code>public class MyExceptionHandler
  {
      [HandleProcessCorruptedStateExceptions]
      public void HandleCorruptedStateException()
      {
          try
          {
               //Force genuine unsafe AccessViolationException
               //not just a throw new AccessViolationException
          }
          catch(Exception e)
          {
               //Log/cleanup/other
          }
      }
  
      public void DoesNotHandleCorruptedStateException()
      {
          try
          {
               //Force genuine unsafe AccessViolationException
               //not just a throw new AccessViolationException
          }
          catch (Exception e)
          {
              //Log/cleanup/other
          }
      }
  }
  </code></pre>
  
  <p>A Test:</p>
  
  <pre><code>class MyTest
  {
      [Test]
      public void ShouldVerifyThatAnAccessViolationExceptionIsHandledCorrectly()
      {
          var handler = new MyExceptionHandler();
  
          Assert.DoesNotThrow(() =&gt; handler.HandleCorruptedStateException());
      }
  
      [Test]
      public void ShouldVerifyThatAnAccessViolationExceptionIsNotHandledCorrectly()
      {
          var handler = new MyExceptionHandler();
  
          Assert.Throws&lt;AccessViolationException&gt;(() =&gt; handler.DoesNotHandleCorruptedStateException());
      }
  }
  </code></pre>
  
  <p>Does anyone have a suggestion of how to achive this without to much work (ex. writting a unsafe lib which causes this exception).</p>
  
  <p>Kind regards</p>
  
  <p><strong>UPDATED:</strong>   To match my final solution, thanks to JaredPar.</p>
  
  <pre><code>public class MyExceptionHandler
  {
      [HandleProcessCorruptedStateExceptions]
      public void HandleCorruptedStateException()
      {
          try
          {
              var ptr = new IntPtr(42);
              Marshal.StructureToPtr(42, ptr, true);
          }
          catch(Exception e)
          {
               //Log/cleanup/other
          }
      }
  
      public void DoesNotHandleCorruptedStateException()
      {
          try
          {
              var ptr = new IntPtr(42);
              Marshal.StructureToPtr(42, ptr, true);
          }
          catch (Exception e)
          {
              //Log/cleanup/other
          }
      }
  }
  </code></pre>
  
  <p><strong>TIP:</strong>
  To verify this manually use a simple console app, from the commandline:</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          var handler = new MyExceptionHandler();
  
          if (args.Length &gt; 1)
          {
              handler.HandleCorruptedStateException();
          }
          else
          {
              handler.DoesNotHandleCorruptedStateException();
          }
      }
  }
  </code></pre>