WebException没有被catch语句捕获

时间:2012-05-22 13:30:41

标签: c# exception-handling invoke begininvoke

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

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

1 个答案:

答案 0 :(得分:2)

如果您使用的是visual studio,请务必转到Debug> Exceptions并捕获常规异常并将其抛出。