ExecuteNonQuery() exception not caught by try-catch

雨燕双飞 提交于 2019-12-11 13:11:17

问题


I am having a problem with a database call that throws an AccessViolationException when I call ExecuteNonQuery(). The call is enclosed in a try-catch block but the exception is never caught. Instead, I get an entry about it in the Windows Event log. Is there a way of catching this exception in code?

IDbCommand cmd = ...
cmd.CommandText = "...";
try
{
    var results = command.ExecuteNonQuery();
}
catch (Exception ex)
{
    Console.Writeline("Caught exception: " + ex.Message);
}

回答1:


ExecuteNonQuery() can throw an AccessViolationException if an underlying driver crashes in native mode. Starting with the .NET Framework 4, managed code no longer catches these types of exceptions in catch blocks. You can read more about this here.

The solution is to either set the <legacyCorruptedStateExceptionsPolicy> element's enabled attribute to true in App.config, or to apply the [System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptionsAttribute] attribute to the method containing the try-catch block.



来源:https://stackoverflow.com/questions/27383472/executenonquery-exception-not-caught-by-try-catch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!