Question

On the basis of what is written on this web page, and if I understand correctly, the using statement works like a try/finally, so I might mistakenly expect that if an exception occurs in a using statement, it should not crash the program.

However, when the DownloadString method, shown in the example below, throws a WebException, the program crashes.

using (WebClient client = new WebClient())
{
    string response = client.DownloadString(url);
    // ...
}

This is normal, since the using statement does not work like a try/catch/finally, then actually no exception is handled. But then I wonder what is the purpose of the using statement.

UPDATE... Based on responses below, I add the following considerations. Basically, if I need to handle an exception, possible solutions could be as follows.

  1. Put the using statement inside a try/catch block.
  2. Put only the DonwloadString method inside a try/catch block.
  3. Write manually a try/catch/finally block as in the following code sample.

Sample code for the third solution.

WebClient client = new WebClient();
try
{
    string response = client.DownloadString(url);
    // ...
}
catch(Exception ex)
{
    // handle (or ignore) the exception
}
finally
{
    if (client != null)
        client.Dispose();
}
Was it helpful?

Solution

if I understand correctly, the using statement works like a try/finally

Correct.

so I would expect that if an exception occurs in a using statement, it should not crash the program.

Incorrect.

Neither try/finally nor using statements swallow exceptions - if you don't catch the exception, it will propagate up. If it's uncaught, it will usually terminate the process. (There are a few cases where it won't, based on which thread it's in and how the CLR is configured, but that's a different matter.)

But then I wonder what is the purpose of the using statement.

To make it simpler to write code that needs to dispose of resources. That's all. If we didn't have using statements, we'd have a lot of try/finally blocks which just called Dispose... and that would be considerably uglier. (Been there, done that - that was Java until Java 7 introduced the try-with-resources statement.)

OTHER TIPS

A using statement doesn't make all exceptions magically disappear, it just means that the IDisposable.Dispose method is correctly called on the client object if one does occur. This is important for making sure that any unmanaged resources are released.

It will rethrow any exceptions that occur, and you'll still have to handle them in your own code.

See MSDN description of using statement

In short, using ensures that .Dispose() is called even if exception occurs. It does not however stop the exception from doing its thing.

The using statement does NOT have a catch, only a finally and thus disposes of the resource for you. If the using statement were to catch all exceptions and continue running it would be a big hindrance to debugging.

It is simply easier and faster to use and read

see for yourself

var myVar = null
try
{
    my = new Object();
    //Do stuff
}
finally
{
    if(myVar != null)
        myVar.Dispose()
}

vs

using(var myVar = new Object())
{
    //Do stuff
}

It simply to make sure the Dispose is called on that object. The using don't do any exception handling as the try/finally don't either

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top