简体   繁体   English

WebException未在try / catch中捕获

[英]WebException not caught in try/catch

I saw several posts with similar problem but no solution works :/ 我看到了几个有类似问题的帖子,但是没有解决方案:/

I debug a windows service by using a console application. 我通过使用控制台应用程序调试Windows服务。 It executes tasks on website and must be able to collect http code status for create logs. 它在网站上执行任务,并且必须能够收集HTTP代码状态以创建日志。 As you can see, sensitive code is in try/catch. 如您所见,敏感代码在try / catch中。

When I debug (F5), I have a WebException that is not caught. 当我调试(F5)时,我没有捕获到WebException。 When I run (CTRL + F5), the exception's message is write in my console and stops my program. 当我运行(CTRL + F5)时,异常消息将写在控制台中并停止我的程序。 This is my code : 这是我的代码:

public partial class Schedulor : ServiceBase
{
void RunTasks()
    {
        schedulor.Start();
        List<Task> task = new List<Task>();
        foreach (TaskPlanner tp in listTp)
        {
            if (tp.CountDown == 0 && tp.IsRunning == false)
            {
                // Initialisation lors de GetTasks()
                tp.IsRunning = true;
                try
                {
                    task.Add(Task.Factory.StartNew(() => tr = tp.ExecuteBot.Execute())); // WEBEXECPTION HERE (cannot find 404)
                }
                catch (Exception e)
                {                        
                    if (e is WebException)
                    {
                        // treatment
                    }                       
                }                    
            }
        }
        Task.WaitAll(task.ToArray());
        CreateLogs();
    }
  }


 public class Bot : IBot
 {
 public TaskResult Execute()
    {
        TaskResult tr = new TaskResult();
        int codeResponse, timeout;
        string credentials;
        try
        {
            WebRequest wrSettings = WebRequest.Create(settings.Url);

            // treatment
        }
        catch (Exception e)
        {
            //Console.WriteLine(e.Message);                
            if (e is WebException)
            {
                var code = ((HttpWebResponse)((WebException)e).Response).StatusCode;
                if ((int)code != settings.HttpResponse)
                {
                    tr.MyResult = TaskResult.Result.nok;
                    goto next;
                }
                else tr.MyResult = TaskResult.Result.ok;
            }
        }            
    next:
        return tr;
    }
 }

I do not understand why my catch does not work. 我不明白为什么我的钓钩不起作用。 I need to treat this information because the task can test if a website return 404 or anything else. 我需要处理此信息,因为该任务可以测试网站是否返回404或其他任何内容。

Thanks in advance 提前致谢

EDIT : ----------- 编辑:-----------

I reduce code as it requests because deleted code does not the real problem 我按要求减少代码,因为删除的代码不是真正的问题

You should catch that exception in task . 您应该在task中捕获该异常。 Add another method, and create your tasks similar to: 添加另一种方法,并创建类似于以下内容的任务:

task.Add(Task.Factory.StartNew(() =>  Process(tp)));

void Process(TaskPlanner tp)
{
    try
    {
        tp.ExecuteBot.Execute();
    }
    catch (WebException wex)
    {
    }
}

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

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