抛出AccessViolationException时,不执行C#catch块

时间:2014-07-07 11:38:07

标签: c# visual-studio-2012 try-catch marshalling access-violation

在下面的代码中,我想使用Marshal读取数据。函数本身工作正常(只要长度适合指针当然)。 我写了一个单元测试来检查copyDataFromMemory的一些可能的结果,并注意到AccessViolationException被抛出,但我的测试在那里停止并且不执行我的catch块。这是我的代码:

private static double[] copyDataFromMemory(PointerLengthPair pointerLengthPair)
{
     double[] dataChain = new double[pointerLengthPair.Length];
     try
     {
         Marshal.Copy(pointerLengthPair.Pointer, dataChain, 0, pointerLengthPair.Length);
     }
     catch (AccessViolationException)
     {        
         return null;
     }
     catch (Exception)
     {
         return null;
     }

     return dataChain;
}

public class PointerLengthPair
{
    public IntPtr Pointer { get; private set; }
    public int Length { get; private set;}

    public PointerLengthPair(IntPtr pointer, int length)
    {
        Pointer = pointer;
        Length = length;
    }
}

现在我的问题是:Visual Studio 2012是否存在某种问题?是否有我必须选择的设置,或者是否无法捕获AccessViolationException?或者我会错过一些明显的东西吗?

(注意:请忽略我目前正在使用null作为返回值。我会尽快将其替换为新类的对象)

0 个答案:

没有答案
相关问题