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

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

问题描述

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

  IDbCommand cmd = ... 
cmd.CommandText = ...;
try
{
var results = command.ExecuteNonQuery();
}
catch(异常例外)
{
Console.Writeline(捕获的异常: + ex.Message);
}


解决方案

如果基础驱动程序在纯模式下崩溃,则ExecuteNonQuery()可以引发 AccessViolationException 。从.NET Framework 4开始,托管代码不再在catch块中捕获这些类型的异常。您可以在此处了解更多信息。



解决方案是设置< legacyCorruptedStateExceptionsPolicy> 元素 enabled 属性在App.config中为 true ,或应用 [System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptionsAttribute]
。handleprocesscorruptedstateexceptionsattribute.aspx rel = nofollow noreferrer>归因于

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() 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.

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

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