Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error catching with webexception

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?

like image 466
Nathan Avatar asked Nov 03 '11 08:11

Nathan


People also ask

How does try catch Work C#?

The C# try and catch keywords are used to define a try catch block. A try catch block is placed around code that could throw an exception. If an exception is thrown, this try catch block will handle the exception to ensure that the application does not cause an unhandled exception, user error, or crash the application.

What is the use of try and catch?

The try statement allows you to define a block of code to be tested for errors while it is being executed. The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.


1 Answers

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

like image 55
fluent Avatar answered Nov 14 '22 23:11

fluent