1
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();
}

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

4

1 回答 1

2

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

于 2012-05-22T13:31:58.310 回答