try-catch 未捕获 ExecuteNonQuery() 异常 [英] ExecuteNonQuery() exception not caught by try-catch

查看:61
本文介绍了try-catch 未捕获 ExecuteNonQuery() 异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我调用 ExecuteNonQuery() 时,我遇到了一个数据库调用的问题,该调用抛出了一个 AccessViolationException.该调用包含在 try-catch 块中,但从未捕获到异常.相反,我在 Windows 事件日志中得到了一个关于它的条目.有没有办法在代码中捕获这个异常?

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);
}

推荐答案

ExecuteNonQuery() 如果底层驱动程序在本机模式下崩溃,则可以抛出 AccessViolationException.从 .NET Framework 4 开始,托管代码不再在 catch 块中捕获这些类型的异常.您可以在此处阅读更多相关信息.

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.

解决方案是设置 elementenabled 属性设置为 true,或应用 [System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptionsAttribute] 属性到包含try-catch 块.

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.

这篇关于try-catch 未捕获 ExecuteNonQuery() 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆