简体   繁体   English

空捕获,但仍引发异常

[英]Empty catch but exception is still thrown

Have a look at the following source code 看看下面的源代码

    private IEnumerable<string> GetAntivirusSoftwareFromNamespace(string @namespace)
    {
        try
        {
            var wmipathstr = string.Format(@"\\{0}{1}", Environment.MachineName, @namespace);
            var searcher = new ManagementObjectSearcher(wmipathstr, "SELECT * FROM AntivirusProduct");
            return from ManagementObject managementObject in searcher.Get()
                select managementObject.Properties["displayName"].Value.ToString();
        }
        catch
        {
            // ignored
        }
        return Enumerable.Empty<string>();
    }

And I call it with the following piece of code 我用下面的代码来称呼它

        antivirusSoftware.AddRange(GetAntivirusSoftwareFromNamespace(@"\root\SecurityCenter"));
        antivirusSoftware.AddRange(GetAntivirusSoftwareFromNamespace(@"\root\SecurityCenter2"));

As you can see, the expcetion is ignored - which works fine on my computer. 如您所见,expcetion被忽略-在我的计算机上可以正常工作。 However if I run it on my staging server (which is Windows Server 2016 Standard) the exception is still thrown. 但是,如果我在我的登台服务器(Windows Server 2016 Standard)上运行它,仍然会引发异常。 Here's the stack trace 这是堆栈跟踪

Unbehandelte Ausnahme: System.Management.ManagementException: Ungültige Klasse bei System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode) bei System.Management.ManagementObjectCollection.ManagementObjectEnumerator.MoveNext() bei System.Linq.Enumerable.d__94`1.MoveNext() bei System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext() 异常情况:System.Management.ManagementException:管理系统中的异常System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
bei System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection) bei protocol.client.Service.Modules.Information.InformationService.GetAntivirusSoftware() in C:\\TeamCity\\buildAgent\\work\\a41c7d46cc4907de\\src\\client\\Service\\Modules\\Information\\InformationService.cs:Zeile 70. bei System.Collections.Generic.List`1.InsertRange(Int32索引,IEnumerable`1集合)bei protocol.client.Service.Modules.Information.InformationService.GetAntivirusSoftware()在C:\\ TeamCity \\ buildAgent \\ work \\ a41c7d46cc4907de \\ src \\ client \\ Service \\ Modules \\ Information \\ InformationService.cs:Zeile 70。

Any idea what could be the cause on this or how to fix it? 知道这可能是什么原因或如何解决? I don't have visual studio or a debugger installed on the staging server. 我在登台服务器上未安装Visual Studio或调试器。

The exception isn't thrown within the method you've shown - you're basically returning a query to the caller, and that query is then failing. 您所显示的方法中不会引发异常-您基本上是将查询返回给调用方,然后该查询失败。

One option would be to force the query to execute immediately by calling ToList() . 一种选择是通过调用ToList()强制查询立即执行。 That way, any exceptions would be thrown within the method itself, so you could catch them: 这样,任何异常都会在方法本身内引发,因此您可以捕获它们:

return searcher.Get()
               .Select(mo => mo.Properties["displayName"].Value.ToString())
               .ToList();

Having said that, I'd strongly advise against having such an empty, blanket catch block - that basically says, "I don't care what's gone wrong, I want my code to keep running regardless, and I don't even want to log what the problem is." 话虽这么说,我强烈建议不要有一个空的,笼罩的捕获块-基本上是说:“我不在乎什么问题, 无论如何我都希望我的代码继续运行,而且我甚至不想记录问题所在。” If you want to guard against ManagementException , catch that specific exception instead - and consider logging it so you can try to avoid it in future. 如果要防止ManagementException ,请改捕获该特定异常-考虑将其记录下来,以便将来避免使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM