title: How to handle System.AccessViolationException?
body: <p>I'm using an external C library in my program coded in C++/CLI with the .NET framework 4. Sometimes the lib crashes and I get the message:</p>
  
  <blockquote>
    <p>Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory.</p>
  </blockquote>
  
  <p>So I tried to handle it with a try/catch block:</p>
  
  <pre><code>try
  {
    Init(); //&lt; lib call which sometimes crashes
  }
  catch (Exception^ e)
  {
    // handle the error
  }
  </code></pre>
  
  <p>But the exception remains uncaught: my program crashes before entering the catch block.</p>
  
  <p>How can I handle this exception to prevent my program from crashing?</p>