6

I'm using a simple webclient to retrieve some XML from a web service, I have this encased in a simple try, catch block (catching WebException). Like the following;

try
        {
            WebClient client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
            client.DownloadStringAsync(new Uri("http://ip/services"));
        }
        catch (WebException e)
        {

            Debug.WriteLine(e.Message);
        }

No if i change the IP address to one that isn't valid, i would of expected it to throw an exception and output the message to the debug window. But it doesn't, it seems the catch block isn't even getting executed. Nothing appears and the debug windows apart from the following;

A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
A first chance exception of type 'System.Net.WebException' occurred in System.Windows.dll
A first chance exception of type 'System.Net.WebException' occurred in System.Windows.dll

My code looks right to me so I can't understand why exceptions are not being caught?

gideon
  • 19,329
  • 11
  • 72
  • 113
Nathan
  • 2,461
  • 4
  • 37
  • 48

2 Answers2

8

From your description of the error messages I would assume that the actual exception thrown is of type "FileNotFoundException".

Have you tried just catching the exception and checking the type? It may be that the web exception is an inner exception.

        try
        {
            WebClient client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
            client.DownloadStringAsync(new Uri("http://ip/services"));
        }
        catch (Exception ex)
        {

            Debug.WriteLine(ex.GetType().FullName);
            Debug.WriteLine(ex.GetBaseException().ToString());
        }

UPDATE : I just noticed that what you are actually calling is an async method.

As a sanity check I would suggest swapping to the non async method and checking the error produced by that.

WebClient.DownloadString Method (Uri)

You may also benefit from looking at this page which walks through catching async errors using the web client as an example.

Async Exceptions

fluent
  • 2,393
  • 4
  • 22
  • 32
  • Still the same, even if i put a simple Debug.WriteLine("test"); in the catch block it doesnt get executed which would suggest the catch block isn't being executed. Thanks – Nathan Nov 03 '11 at 09:19
  • Answer updated as I noticed that you are calling an async method – fluent Nov 03 '11 at 09:23
  • ahh thank you! i didnt know you had to catch exceptions differently when doing things async. I didnt use the method you posted (via the link), but instead checked for errors in the DownloadStringCompleted which is working great. Thanks for putting me on to the answer! – Nathan Nov 03 '11 at 09:51
3

The exception will never be raised from DownloadStringAsync. It simply won't throw it, but the DownloadString (non Async) will throw it. I don't know if this is a bug, I thought the async methods never throw exceptions apart from ArgumentException, though the documentation states otherwise.

You have to "catch" the error in the DownloadStringCompletedEventHandler:

void DownloadStringCompletedEventHandler(object sender, DownloadStringCompletedEventArgs e)
{
    if (e.Error != null)
    {
        Debug.WriteLine(e.Error);
        return;
    }

You can almost always safely ignore "first chance" exceptions, those are caught within the framework and handled accordingly. See this question for some more info on that.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272