Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Desktop Developer

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Parsing text with Piglet making the first tests passWhat does the Using statement do? »
    comments

    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.

    1. Dim s2 = New FileStream("test2", FileMode.Create)
    2.         Try
    3.             Console.WriteLine("before exception")
    4.             Throw New Exception()
    5.             Console.WriteLine("after exception")
    6.         Catch ex As Exception
    7.  
    8.         Finally
    9.             s2.Dispose()
    10.         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.

    1. Using s1 = New FileStream("test1", FileMode.Create)
    2.             Console.WriteLine("before exception")
    3.             Throw New Exception()
    4.             Console.WriteLine("after exception")
    5.         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.

    1. Dim s2 = New FileStream("test2", FileMode.Create)
    2.         Try
    3.             s2 = Nothing
    4.         Finally
    5.             s2.Dispose()
    6.         End Try

    would have given you this.

    but the using statement does not cause this.

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

    Conclusion

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

    About the Author

    User bio imageChris is awesome.
    Social SitingsTwitterHomePageLTD RSS Feed
    using, vb.net
    InstapaperVote on HN

    1 comment

    Comment from: Benno [Visitor]
    Benno I'm not too happy with the check for Nothing/null.

    As the creation of the object happens outside the try/finally (a good thing), the object is not Nothing/null inside the try/finally.
    So it is the responsibility of the programmer not to nil the object before the finally.

    In fact, the way it is implemented now (and has been since .NET 2.0 for VB.NET) it intercepts a possible exception in your code, which is imho almost as bad as swallowing an exception.

    By the way, the "equivalen code" bit in the 2005 article is (and was) indeed wrong.
    It was my teacher in evenening school, Dirk Andries, who showed me the IL code using Lutz Roeder's .NET Reflector for the using statement back somewhere in 2004. That's why I remembered there was no catch generated.
    10/21/12 @ 10:00

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)