471,315 Members | 1,695 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,315 software developers and data experts.

USING clause with TRY...CATCH blocks

Consider the following, where Foo implements IDisposable:

try
{
using (Foo myFoo = new Foo())
{
myFoo.Execute();
}
}
catch (someException)
{
// some stuff
}
finally
{
// some other stuff
}

If the call to Foo.Execute() throws an exception, is Foo displosed? My
understanding is that it would be if the TRY...CATCH is inside the USING, but
what happens in the example given above?

Thanks in advance.

/Joel Finkel
Jan 9 '06 #1
5 1828
Joel,
If the call to Foo.Execute() throws an exception, is Foo displosed?


Do you mean is myFoo disposed? If so the answer is yes, even without
the outer try/catch block. That's the whole point of the using
statement.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jan 9 '06 #2
Thank you. I wanted to ensure that the code did not branch around the
implicit Dispose by jumping directly to the exception handling.

"Mattias Sjögren" wrote:
Joel,
If the call to Foo.Execute() throws an exception, is Foo displosed?


Do you mean is myFoo disposed? If so the answer is yes, even without
the outer try/catch block. That's the whole point of the using
statement.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Jan 9 '06 #3
Hi Joel,

a little "behind-the-scenes" might further clarify this: the C#-compiler
translates your using-block into a try-finally block containing the original
statements inside the using block, so

using (Foo myFoo = new Foo())
{
myFoo.Execute();
}

becomes:
try
{
myFoo.Execute();
}
finally
{
myFoo.Dispose();
}

If an exception should occur within the statements originally within the
using-block, this exception will bubble up, but only after running the
finally-section disposing your object.

HTH,
Baileys

"Joel Finkel" wrote:
Thank you. I wanted to ensure that the code did not branch around the
implicit Dispose by jumping directly to the exception handling.

"Mattias Sjögren" wrote:
Joel,
If the call to Foo.Execute() throws an exception, is Foo displosed?


Do you mean is myFoo disposed? If so the answer is yes, even without
the outer try/catch block. That's the whole point of the using
statement.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Jan 10 '06 #4
_DS
On Mon, 9 Jan 2006 22:20:02 -0800, "Baileys"
<Ba*****@discussions.microsoft.com> wrote:
Hi Joel,

a little "behind-the-scenes" might further clarify this: the C#-compiler
translates your using-block into a try-finally block containing the original
statements inside the using block


With this in mind, I'm surprised that 'using' is preferred over
try/finally. Yes, it looks simpler for opening one file...IF the file
open or subsequent ops are guaranteed not to generate an exception.
But if an exception is generated, isn't it better to have explicit
handlers in the finally block?

Also multiple file opens seem more awkward with 'using' statements.
I know that you could add multiple opens to one 'using' but I'm not
sure if the exact handling if, say, file #3 out of 5 generates an
exception. I would imagine that the compiler generates code that
knows to close/dispose just the first two files. But perhaps for that
reason, I've seen experts write nested 'using' blocks. Not pretty.

Couldn't the mutiple opens be more gracefully handled in a try-finally
block?

Jan 17 '06 #5
_DS wrote:
a little "behind-the-scenes" might further clarify this: the C#-compiler
translates your using-block into a try-finally block containing the original
statements inside the using block
With this in mind, I'm surprised that 'using' is preferred over
try/finally. Yes, it looks simpler for opening one file...IF the file
open or subsequent ops are guaranteed not to generate an exception.
But if an exception is generated, isn't it better to have explicit
handlers in the finally block?


Not unless you want to do something other than calling Dispose on the
stream.
Also multiple file opens seem more awkward with 'using' statements.
I know that you could add multiple opens to one 'using' but I'm not
sure if the exact handling if, say, file #3 out of 5 generates an
exception. I would imagine that the compiler generates code that
knows to close/dispose just the first two files. But perhaps for that
reason, I've seen experts write nested 'using' blocks. Not pretty.
I generally use nested using blocks, although you can nest without
having extra braces:

using (...)
using (...)
{
code
}
Couldn't the mutiple opens be more gracefully handled in a try-finally
block?


Which do you think is simpler:

Stream first = null;
Stream second = null;
try
{
first = new FileStream (...);
second = new FileStream (...);
// Code
}
finally
{
if (first != null)
{
first.Dispose();
}
if (second != null)
{
second.Dispose();
}
}

or:

using (Stream first = new FileStream (...))
using (Stream second = new FileStream(...))
{
// Code
}

I know which I prefer...

Jon

Jan 17 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Andreas Müller | last post: by
1 post views Thread by gemel | last post: by
1 post views Thread by Anonieko | last post: by
3 posts views Thread by Rahul Babbar | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.