C# keyword: 'Using' - what's up with that?

  • Thread starter Thread starter Graham
  • Start date Start date
G

Graham

Are programmers out there really using the 'using'
keyword?

C# has the 'using' keyword which will provide useful
scoping for an object that ought to be 'disposed' after
being used. I'm questioning the practical use of this
construct...

By using this keyword, the claim is that the code is
cleaner, which I would agree with. However, realize that
any exceptions thrown in your embedded statements will go
undetected unless you actually add your own exception
handling. Consider the following:

using (type variable = initialization)
{
embeddedStatement;
}

which translates to:

{
type variable = initialization; // done inside brace scope
try
{
embeddedStatement;
}
finally
{
if (variable != null)
{
((IDisposable)variable).Dispose(); // the magic
}
}
} // end scope

Note that any exceptions that occur in the
EmbeddedStatement are simply swallowed and go undetected.
In order to catch the exceptions, I would have to write
the following:

using (type variable = initialization)
{
try {embeddedStatement;}
catch (Exception x) { //do something; }
}

Given that I need to add the try/catch block, what's the
point of using 'using' [sic]? By definition, I'm using the
keyword because I want to invoke Dispose. If I know that
already, AND I'm adding a try/catch block, why bother
with 'using' at all? - Just add the finally clause to the
try/catch block - especially if there is other cleanup
that needs to be done!

I can see some benefit with the 'using' keyword, but I
believe its usage may hide exceptions if the programmer
isn't careful. - And some of you reading this may go back
to your code to check :)


Cheers
graham
 
Graham said:
Are programmers out there really using the 'using'
keyword?

Absolutely - all the time. I've been considering how it would best be
implemented in Java, too - we've had some discussions about it on
comp.lang.java.programmer. It's lovely.
C# has the 'using' keyword which will provide useful
scoping for an object that ought to be 'disposed' after
being used. I'm questioning the practical use of this
construct...

By using this keyword, the claim is that the code is
cleaner, which I would agree with. However, realize that
any exceptions thrown in your embedded statements will go
undetected unless you actually add your own exception
handling. Consider the following:

using (type variable = initialization)
{
embeddedStatement;
}

Note that any exceptions that occur in the
EmbeddedStatement are simply swallowed and go undetected.

No they're not. They're propagated up. If you don't believe me, try
this code:

using System;
using System.IO;

public class Test
{
static void Main()
{
string x = null;

using (FileStream fs = new FileStream ("test.txt",
FileMode.Append))
{
Console.WriteLine ("Before exception");
int y = x.Length; // Generate an exception
Console.WriteLine ("After exception");
}
Console.WriteLine ("After using");
}
}

If on your system the "After exception" and "After using" appear, it's
broken. On my system, "Before exception" is printed out, then the
exception is thrown and breaks out of Main.

I'm confused as to why you think it would be swallowed - finally blocks
don't swallow exceptions, and there's no catch in the expanded code you
posted - so why would an exception be swallowed?
 
Graham said:
Are programmers out there really using the 'using'
keyword?
Sure am! I can now write a short, to the point bit of code that will go get
stuff from the database, and either succeed or throw whatever exception was
reported during the database access. However it returns, database resources
are correctly released.

As a Java programmer, I think this a very elegant innovation.
By using this keyword, the claim is that the code is
cleaner, which I would agree with. However, realize that
any exceptions thrown in your embedded statements will go
undetected unless you actually add your own exception
handling.

No, exceptions are propogated. See Jon's post

Regards

Ron
 
Back
Top