2010-12-03 147 views
4

如何捕捉在try-catch块的AccessViolation例外:处理AccessViolation异常C#

这里是下面的代码:

public static BP GetBloodPressure(string vendorid, string productid) 
{ 
    BP Result = new BP(); 
    try 
    { 
     GETBPData BPreadings = new GETBPData(); 
     UInt16 VendorId = Convert.ToUInt16(vendorid, 16); 
     UInt16 ProductId = Convert.ToUInt16(productid, 16); 

     if (HealthMonitorData.HidDataTap_GetBloodPressure(VendorId, ProductId, ref BPreadings)) // error here 
     { 

      if (BPreadings.ucSystolic == 0 && BPreadings.ucDiastolic == 0 && BPreadings.DeviceId1 == 0 && BPreadings.DeviceId2 == 0 && BPreadings.ucPulse == 0) 
      { 
       Result = null; 

      } 
      else 
      { 
       Result.UcSystolic = BPreadings.ucSystolic; 
       Result.UcDiastolic = BPreadings.ucDiastolic; 
       Result.UcPulse = BPreadings.ucPulse; 
       Result.DeviceId1 = BPreadings.DeviceId1; 
       Result.DeviceId2 = BPreadings.DeviceId2; 
      } 
     } 
    } 
    catch (Exception ex) 
    { 

    } 
     return Result; 
} 

我导入一个DLL来读取血压值从设备。我试图捕获异常,但控制不会超出访问冲突异常即将发生时的“if”语句。

请建议?

感谢AccessViolationExceptions的

+1

修复你的代码,不追赶这些例外。 – leppie 2010-12-03 05:50:23

+0

也,如何解决我的代码使用外部的DLL。 – Tarun 2010-12-03 06:29:04

回答

8

处理等corrupted state exceptions已在.NET 4被改变一般来说,你应该捕获这些异常,所以运行时间已更改为反映这一点。如果您确实需要了解这些信息,则必须使用HandledProcessCorruptedStateExceptions属性注释代码。

请记住,行为发生了很好的变化。大多数应用程序不能以任何有意义的方式处理这些异常,因此不应该抓住它们。

2

其HandleProcessCorruptedStateExceptions不HandleDProcessCorruptedStateExceptions