任务中的异常跟踪

时间:2014-05-21 14:40:43

标签: c#

我很难在Task内跟踪异常。从异常中返回的消息根本没有帮助。以下是似乎抛出异常的方法:

 public async Task<bool> TestProxy(IPEndPoint proxy)
 {
    Ping ping = new Ping();
    PingReply reply = await ping.SendPingAsync(proxy.Address);

    if (reply == null || reply.Status != IPStatus.Success) return false;

    HttpClient client = new HttpClient();

    HttpResponseMessage response;

    try
    {
        response = await client.GetAsync("https://google.com");
    } catch(Exception ex)
    {
        return false;
        Console.WriteLine(ex.Message);
    }

    if(response.IsSuccessStatusCode)
    {
        return true;
    }
    else
    {
        return false;
    }

这是调用代码:

public async Task<List<IPEndPoint>> FetchWorkingProxiesAsync()
{
    List<IPEndPoint> _proxies = await FetchRawProxiesAsync();
    List<IPEndPoint> workingProxies = new List<IPEndPoint>();

    Task[] tasks = new Task[_proxies.Count];

    for(int i = 0; i < _proxies.Count; i++)
    {
        IPEndPoint _tmpProxy = _proxies[i];

        tasks[i] = Task.Run(async () => {
            try
            {
                if (await TestProxy(_tmpProxy))
                {
                    workingProxies.Add(_tmpProxy);
                }
            }catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        });
    }

    await Task.WhenAll(tasks);

    return workingProxies;
}

抛出的异常是:

InnerException {"The request was aborted: The request was canceled."}   System.Exception {System.Net.WebException}

当我设置断点时,没有关于异常的有用信息。我看到那里有一个System.Net.WebException,但它并没有说出什么或为什么。

我在VS中的例外对话框中勾选了CLR投掷和用户例外。

修改:我刚刚意识到我在尝试连接google.com时没有使用代理来测试连接。即便如此,我仍然想知道如何更多地了解抛出的异常。

0 个答案:

没有答案