为什么会这样捕获所有的块实际上不是捕获所有 [英] Why would this catch all block not in fact catch all

查看:148
本文介绍了为什么会这样捕获所有的块实际上不是捕获所有的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在code是相当简单---问题是,有无效字符的字符串groupPath(A'/'是精确的)。

The code is fairly simple --- the issue is that there is an invalid character in the groupPath string (a '/' to be exact).

我想要做的(至少一个权宜)是跳过DirectoryEntries,我不能得到CN的---不管是为什么。

What I'm trying to do (at least as a stop gap) is skip over DirectoryEntries that I can't get the cn for --- regardless of why.

然而,当我运行此$ C C catch块$不跑,我也得到改为: 该服务器不可操作。和未处理的System.Runtime.InteropServices.COMException。

However when I run this code the catch block doesn't run and I get instead: The server is not operational. and an unhandled System.Runtime.InteropServices.COMException.

为什么catch块未捕获此异常。

Why would the catch block not catch this exception.

try
{
    using (DirectoryEntry groupBinding = new DirectoryEntry("LDAP://" + groupPath))
    {
        using (DirectorySearcher groupSearch = new DirectorySearcher(groupBinding))
        {

            using (DirectoryEntry groupEntry = groupSearch.FindOne().GetDirectoryEntry())
            {
                results.Add(string.Format("{0}", groupEntry.Properties["cn"].Value.ToString()));
            }
        }
    }
}
catch
{
    Logger.Error("User has bad roles");
}

补充意见: 在code其实是一个自定义RoleProvider,而奇怪的是,如果我引用,此提供一个简单的WinForms应用程序,并调用相同的方法与catch块不正是它该做相同的输入。我认为,这表明有关.NET异常与COM异常所提出的答案是不准确的。 虽然我很茫然理解为什么当从Webdev的服务器执行此code就赶不上

Additional observations: The code is actually in a custom RoleProvider, and the curious thing is that if I reference, this provider in a simple winforms app, and call this same method with the same inputs the catch block does exactly what it's suppose to do. I think this suggests that the proposed answer concerning .NET exceptions versus COM exceptions is not accurate. Although I am at a loss to understand why this code would not catch when executed from the WebDev server

推荐答案

在没有指定什么抓,则默认为.NET异常。您的例外是COM .NET的地方没有设置捕获异常。对付这一点的最好办法是捕捉COM异常,这应该是这个样子:

When you don't specify what to catch, it defaults to .NET exceptions. Your exception is in COM where .NET isn't set to catch the exception. The best way to deal with this is to catch the COM exception, which should look something like this:

    try
    {

    }
    catch (System.Runtime.InteropServices.COMException COMex)
    {

    }
    catch (System.Exception ex)
    {

    }

这篇关于为什么会这样捕获所有的块实际上不是捕获所有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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