C# “System.Net.WebException”类型的第一次机会异常发生在 System.dll 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10707712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 14:55:21  来源:igfitidea点击:

A first chance exception of type 'System.Net.WebException' occurred in System.dll

c#visual-studio-2010debugging

提问by Only Bolivian Here

I'm using TweetSharp to find the followers for a user.

我正在使用 TweetSharp 查找用户的关注者。

Here is the code:

这是代码:

public static void FindFollowersForUser(TwitterUserModel twitterUser)
{
    try
    {
        var followers = service.ListFollowersOf(twitterUser.TwitterName, -1);
        if (followers == null) return;
        while (followers.NextCursor != null)
        {
            var foundFollowers = service.ListFollowersOf(twitterUser.TwitterName, (long)followers.NextCursor);
            if (foundFollowers == null) continue;

            Debug.WriteLine("Followers found for: " + twitterUser.TwitterName);
            foreach (var follower in foundFollowers)
            {
                twitterUser.Followers.Add(follower.ScreenName);
            }
        }
    }
    catch (WebException e)
    {
        throw e;
    }
}

I've tried wrapping the code in a try/catch, to catch the WebException error being fired and review it's InnerException, but the catch is never entered despite the error message being shown in the output window (View -> Output) in Visual Studio.

我尝试将代码包装在 try/catch 中,以捕获正在触发的 WebException 错误并查看它的 InnerException,但是尽管View -> OutputVisual Studio的输出窗口 ( ) 中显示了错误消息,但从未输入过该捕获。

How can I see the inner exception of this breaking bug? This is the first time I've seen the debugger not firing the catch when an exception is fired.

我如何才能看到这个破坏性错误的内部异常?这是我第一次看到调试器在触发异常时不触发捕获。

采纳答案by Peter Ritchie

I assume when you say "First chance exception" you mean the message that is output to the Debug console? That message is output whenever an exception is thrown. The exception may be caught by code and handled and not allowed to propagate up the stack. TweetSharp may be catching this exception within its code and handling in some way so it never reaches your catchblock

我假设当您说“第一次机会异常”时,您的意思是输出到调试控制台的消息?每当抛出异常时,就会输出该消息。异常可能会被代码捕获并处理,并且不允许向上传播。TweetSharp 可能会在其代码中捕获此异常并以某种方式进行处理,因此它永远不会到达您的catch阻止

This is normal and only the debugger displays this message. If this is a problem for you in some way (other than the message displaying in the Output window), please provide more detail.

这是正常现象,只有调试器会显示此消息。如果这对您来说是个问题(除了输出窗口中显示的消息),请提供更多详细信息。

回答by Mikko Viitala

I was looking something else, really, but this cought my eye. If you are planning to rethrow exception then you want to replace this

我在看别的东西,真的,但这让我不舒服。如果您打算重新抛出异常,那么您想替换它

catch (WebException e) { throw e; }

with this so you won't mess up the stacktrace.

这样你就不会弄乱堆栈跟踪。

catch (WebException e) { throw; }