为什么我的异常处理代码不处理异常?

时间:2021-08-04 23:27:16

I have some C# code that pulls down a remote website using the HttpWebRequest class. I'm handling errors with a try/catch, but some errors (like Webrequest and IOException) don't seem to be getting "caught" with the way I have it setup:

我有一些C#代码使用HttpWebRequest类来下载远程网站。我正在使用try / catch处理错误,但是有些错误(比如Webrequest和IOException)似乎没有被我设置的方式“抓住”:

try
{
    StartScrap("http://www.domain.com");
}
catch (Exception ex)
{
    LogError(ex.ToString();
}


private void StartScrap(string url)
{
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);

    HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();

    if (response.StatusCode == HttpStatusCode.OK)
    {
        Stream responseStream = response.GetResponseStream();
        string responseText = String.Empty;

        using (StreamReader readerStream = new StreamReader(responseStream, System.Text.Encoding.UTF8))
        {
            responseText = readerStream.ReadToEnd(); <-- I will sometimes get a Webexception error here that won't get caught above and stops the code
        }
    }
}

Update: There is more to the code, so maybe it is something outside of the code I posted? I am basically using this code in a Windows Application on a form that has a NotifyIcon. I'm using the Timer class to run the code at a certain timer interval. This is how I have it setup:

更新:代码还有更多,所以它可能是我发布的代码之外的东西吗?我基本上在具有NotifyIcon的表单上的Windows应用程序中使用此代码。我正在使用Timer类以特定的定时器间隔运行代码。这就是我设置它的方式:

    public TrayIcon()
    {
        InitializeComponent();
    }

    private void TrayIcon_Load(object sender, EventArgs e)
    {
        try
        {
            StartScrap("http://www.domain.com");
        }
        catch (Exception ex)
        {
            LogError(ex.ToString());
        }
        finally
        {
            StartTimer();
        }
    }

    private void StartTimer()
    {
        Timer Clock = new Timer();
        Clock.Interval = 600000;
        Clock.Start();
        Clock.Tick += new EventHandler(TrayIcon_Load);
    }

4 个解决方案

#1


3  

What exactly do you mean by "stops the code"? Are you running in a debugger by any chance? My guess is that if you run outside the debugger - or just hit "run" again in the debugger - you'll get into the catch block with no problems. Alternatively, go into the debugger settings and change at which point the debugger steps in.

“停止代码”究竟是什么意思?您是否有机会在调试器中运行?我的猜测是,如果你在调试器之外运行 - 或者只是在调试器中再次点击“run” - 你将进入catch块而没有任何问题。或者,进入调试器设置并更改调试器进入的位置。

Of course, if this isn't happening in the debugger, we just need more information about exactly what you're seeing.

当然,如果在调试器中没有发生这种情况,我们只需要更多关于您所看到的内容的信息。

#2


2  

Could it be that LogError is throwing an exception?

可能是LogError抛出异常吗?

#3


0  

Frankly speaking I am not sure what exactly happening but I will suggest you to go with ELMAH.(Error Logging Modules and Handlers)

坦率地说,我不确定到底发生了什么,但我建议你选择ELMAH。(错误记录模块和处理程序)

Here is a step by step How to for ELMAH.

这是一步一步如何为ELMAH。

#4


0  

Nevermind, I found out I was calling the wrong function my Timer class and it was bypassing the event handler.

没关系,我发现我调用了错误的函数我的Timer类,它绕过了事件处理程序。

#1


3  

What exactly do you mean by "stops the code"? Are you running in a debugger by any chance? My guess is that if you run outside the debugger - or just hit "run" again in the debugger - you'll get into the catch block with no problems. Alternatively, go into the debugger settings and change at which point the debugger steps in.

“停止代码”究竟是什么意思?您是否有机会在调试器中运行?我的猜测是,如果你在调试器之外运行 - 或者只是在调试器中再次点击“run” - 你将进入catch块而没有任何问题。或者,进入调试器设置并更改调试器进入的位置。

Of course, if this isn't happening in the debugger, we just need more information about exactly what you're seeing.

当然,如果在调试器中没有发生这种情况,我们只需要更多关于您所看到的内容的信息。

#2


2  

Could it be that LogError is throwing an exception?

可能是LogError抛出异常吗?

#3


0  

Frankly speaking I am not sure what exactly happening but I will suggest you to go with ELMAH.(Error Logging Modules and Handlers)

坦率地说,我不确定到底发生了什么,但我建议你选择ELMAH。(错误记录模块和处理程序)

Here is a step by step How to for ELMAH.

这是一步一步如何为ELMAH。

#4


0  

Nevermind, I found out I was calling the wrong function my Timer class and it was bypassing the event handler.

没关系,我发现我调用了错误的函数我的Timer类,它绕过了事件处理程序。