【问题标题】:WebException not being caught by catch statement [duplicate]catch 语句未捕获 WebException [重复]
【发布时间】:2012-05-22 13:30:41
【问题描述】:
try
{
    this.Invoke((MethodInvoker)delegate
    {
        Uri uri = new Uri("mywebpage.com");
        NetworkCredential credentials = new NetworkCredential("username", "password");
        byte[] lnBuffer;
        byte[] lnFile;
        HttpWebRequest lxRequest = (HttpWebRequest)WebRequest.Create(uri);
        lxRequest.Credentials = credentials;
        // the line below throws a webexception that for some reason is not being caught by my webexception catch statement.
        using (HttpWebResponse lxResponse = (HttpWebResponse)lxRequest.GetResponse())
        {
            using (BinaryReader lxBR = new BinaryReader(lxResponse.GetResponseStream()))
            {
                using (MemoryStream lxMS = new MemoryStream())
                {
                    lnBuffer = lxBR.ReadBytes(1024);
                    while (lnBuffer.Length > 0)
                    {
                        lxMS.Write(lnBuffer, 0, lnBuffer.Length);
                        lnBuffer = lxBR.ReadBytes(1024);
                    }
                    lnFile = new byte[(int)lxMS.Length];
                    lxMS.Position = 0;
                    lxMS.Read(lnFile, 0, lnFile.Length);
                    lxMS.Close();
                    lxBR.Close();
                }
            }
            lxResponse.Close();
        }

        using (MemoryStream lxFS = new MemoryStream(lnFile))
        {
            lxFS.Write(lnFile, 0, lnFile.Length);
            lxFS.Position = 0;
            Image resizedimage = new Bitmap(Image.FromStream(lxFS), new Size(320, 240));
            pictureBox23.Image = resizedimage;

            //otherform.picboximage = Image.FromStream(lxFS);
            lxFS.Close();
        }
    });
}
catch (WebException e)
{
    this.Close();
}
catch (InvalidOperationException e)
{
    this.Close();
}

上面带有注释的行会引发网络异常,但是我的网络异常捕获语句没有捕获它。有任何想法吗? 谢谢

【问题讨论】:

    标签: c# exception-handling invoke begininvoke


    【解决方案1】:

    如果您使用的是 Visual Studio,请确保转到 Debug>Exceptions 并捕获一般异常并将其抛出。

    【讨论】:

    • 一般例外情况有哪些?通用语言?还是 Win32 异常?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-08
    • 1970-01-01
    • 2013-02-05
    相关资源
    最近更新 更多