【问题标题】:WebException thrown but never gets caught [duplicate]抛出 WebException 但从未被捕获 [重复]
【发布时间】:2012-02-26 18:05:50
【问题描述】:

我有以下代码:

try
{
    using (var myHttpWebResponse = (HttpWebResponse) httPrequestCreated.GetResponse())
    {
        var streamResponse = myHttpWebResponse.GetResponseStream();

        if (streamResponse != null)
        {
            var streamRead = new StreamReader(streamResponse);
            var readBuff = new Char[256];
            var count = streamRead.Read(readBuff, 0, 256);         

            while (count > 0)
            {
                var outputData = new String(readBuff, 0, count);
                finalResopnse += outputData;
                count = streamRead.Read(readBuff, 0, 256);
            }
            streamRead.Close();
            streamResponse.Close();
            myHttpWebResponse.Close();

        }
    }
}
catch (WebException ex)
{
    MessageBox.Show("something went wrong");
}

错误代码是404 Not Found,但我收到以下错误,而不是 MessageBox:

为什么从未捕获到异常?

【问题讨论】:

    标签: c# .net exception exception-handling httpwebrequest


    【解决方案1】:

    您可能第一次在 Visual Studio 中启用了异常捕获。

    尝试在没有调试器的情况下运行应用程序 (Ctrl+F5)。或者,如果您看到此对话框,您可以按 Run (F5) 来获得您的消息框。

    【讨论】:

      【解决方案2】:

      您确定要“捕获”相同类型的异常吗?而不是WebException,只捕获Exception,看看你是否得到MessageBox

          try
          {
              using (var myHttpWebResponse = (HttpWebResponse) httPrequestCreated.GetResponse())
              {
                  var streamResponse = myHttpWebResponse.GetResponseStream();
      
                  if (streamResponse != null)
                  {
                      var streamRead = new StreamReader(streamResponse);
                      var readBuff = new Char[256];
                      var count = streamRead.Read(readBuff, 0, 256);         
      
                      while (count > 0)
                      {
                          var outputData = new String(readBuff, 0, count);
                          finalResopnse += outputData;
                          count = streamRead.Read(readBuff, 0, 256);
                      }
                      streamRead.Close();
                      streamResponse.Close();
                      myHttpWebResponse.Close();
      
                  }
              }
      
          }
          catch (Exception ex)
          {
              MessageBox.Show(string.format("this went wrong: {0}", ex.Message));
          }
      

      编辑:仔细观察你的照片,我认为你在被抛出到你的 catch 块之前看到了异常。在您的 VS 上按 Ctrl+Alt+E 并确保 Common Language Runtime ExceptionsThrow 检查是unchecked

      【讨论】:

      • GetResponse() 确实抛出了WebException,这就是他正在捕捉的,所以我认为这不是问题。
      • 如果他定义了自己的 WebException (MyNamespace.WebException) 并捕获了那个,而不是 System.Net.WebException?
      • 消息与实际System.Net.WebException 完全相同的消息?这不太可能。
      • 我不是说抛出的异常,那个异常是 System.Net.WebException,我说的是他正在捕获的那个......你看不到他使用的命名空间发布的代码(他还是解决了)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-15
      • 2017-04-17
      • 2023-02-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多