如何在 try-catch 块中捕获 AccessViolation 异常:
这是下面的代码:
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 以从设备读取血压值。我已 try catch 异常,但控制并未超出访问冲突异常即将到来的“if”语句。
请推荐?
谢谢
最佳答案
处理 AccessViolationExceptions 和其他 corrupted state exceptions在 .NET 4 中已更改。通常您不应捕获这些异常,因此运行时已更改以反射(reflect)这一点。如果你真的需要捕捉这些,你必须用 HandledProcessCorruptedStateExceptions
注释代码。属性。
请记住,行为的改变是有充分理由的。大多数应用程序将无法以任何有意义的方式处理这些异常,因此不应捕获它们。
关于wpf - 在 try catch c# 中处理 AccessViolation 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4342677/