1

I have the following code:

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

The error code is 404 Not Found, but instead of a MessageBox I get the following error:

enter image description here

Why is the exception never caught?

DavidRR
  • 18,291
  • 25
  • 109
  • 191

2 Answers2

2

You probably have first chance exception catching turned on in Visual Studio.

Try running the application without debugger (Ctrl+F5). Or, if you get this dialog, you can press Run (F5) to get your message box.

svick
  • 236,525
  • 50
  • 385
  • 514
0

Are you sure you are 'catching' the same type of Exception? Instead of WebException catch just the Exception and see if you get the 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));
    }

Edit: Watching you pic closely I think you are seeing the Exception before is being thrown to your catch block. On your VS hit Ctrl+Alt+E and make sure the Throw check of Common Language Runtime Exceptions is unchecked

sebagomez
  • 9,501
  • 7
  • 51
  • 89
  • `GetResponse()` does throw `WebException` and that's what he's catching, so I don't think this is the problem. – svick Feb 26 '12 at 18:14
  • 1
    what if he's defined his own WebException (MyNamespace.WebException) and catching that one instead of System.Net.WebException? – sebagomez Feb 26 '12 at 18:19
  • One that has exactly the same message as the actual `System.Net.WebException`? That's not very likely. – svick Feb 26 '12 at 20:16
  • I'm not saying the thrown exception, that one is obiusly a System.Net.WebException, I'm saying the one he is catching... you can't see the namespaces he's using from the posted code (he solved it anyways) – sebagomez Feb 27 '12 at 13:29