Introduction

In my previous post I examined what the Using code does. And why the 2005 version of the MSDN article might have been wrong.

But why is it important to know that Using is not using a catch?

The catch

Here is the code that we saw in the article.

Dim s2 = New FileStream("test2", FileMode.Create)
        Try
            Console.WriteLine("before exception")
            Throw New Exception()
            Console.WriteLine("after exception")
        Catch ex As Exception

        Finally
            s2.Dispose()
        End Try

The above code will just go merrily on it’s way, it won’t really stop and it is swallowing the exception. The user of that code does not know something bad happened and thinks everything is just fine. The user does not know there is a line after the exception he was supposed to get. He is blissfully unaware.

But we are glad to say that Using does not do that.

So this code.

vbnet Using s1 = New FileStream("test1", FileMode.Create) Console.WriteLine("before exception") Throw New Exception() Console.WriteLine("after exception") End Using will throw an exception.

The isnot nothing

But the isnot nothing bit in the finally statement is useful to know too.

Because this code.

vbnet Dim s2 = New FileStream("test2", FileMode.Create) Try s2 = Nothing Finally s2.Dispose() End Try would have given you this.

but the using statement does not cause this.

vbnet Using s1 = New FileStream("test1", FileMode.Create) s2 = Nothing End Using

Conclusion

Sometimes it pays to look just a bit further and not believe the first hit you get on Google.