无法捕获 Win32Exception

2024-03-11

因此,我在实体框架“任何”事务期间收到 Win32Exception。数据库机器已离线,因此这是可以预料的。这是一个监控应用程序,所以我想记录机器不可用,但它没有捕获错误。

错误是:(Elmah 报告)

System.ComponentModel.Win32Exception 找不到网络路径

System.Data.SqlClient.SqlException (0x80131904): 建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 (提供程序:命名管道提供程序,错误:40 - 无法打开与 SQL Server 的连接) ---> System.ComponentModel.Win32Exception (0x80004005):找不到网络路径 在System.Data.SqlClient.SqlInternalConnection.OnError(SqlException异常,布尔breakConnection,Action`1wrappCloseInAction)

好的,这是我的(伪)代码:

if(Monitor.TryEnter(Variables))
try
{
    foreach(var thing in things)
    {
        try
        {
            using(var ctx = new context())
            {
                Do Stuff... 
                if(thing.Any()) <-- Error
                Do more stuff... like look for problems.
            }
        }
        catch(Win32Exception)
        {
            Add exception to list of problems.;
        }
        catch(exception)
        {
            throw; //I was just seeing if this would help, it didn't
        }
     }
     Return list of problems
}
catch(Exception ex)
{
    Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(ex));
}
finally
{
    Monitor.Exit(variables)
}

所以我希望您能看到 Win32Exception 应该被捕获,但事实并非如此 - Elmah 正在报告它。正在正确定位并报告数据库中发现的错误。这一切都有效,直到这个 Win32Exception 发生,然后它似乎忽略了我的捕获。


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法捕获 Win32Exception 的相关文章

随机推荐