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
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