473,396 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 1894
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Sean Shanny | last post by:
To all, The facts: PostgreSQL 7.4.0 running on BSD 5.1 on Dell 2650 with 4GB RAM, 5 SCSI drives in hardware RAID 0 configuration. Database size with indexes is currently 122GB. Schema for...
10
by: Andreas Müller | last post by:
Hi all, I have a class SwitchCursor, that implements IDisposable. Is it legal to use an object that is not assigned to a reference inside a using statement like this: using(new...
8
by: Z D | last post by:
Hi, I was wondering what's the point of "finally" is in a try..catch..finally block? Isn't it the same to put the code that would be in the "finally" section right after the try/catch block?...
5
by: Miyra | last post by:
Hi. I'm working with an app that uses exceptions for control flow. These are code blocks where exceptions are thrown/caught regularly. A couple hundred exceptions occur per hour and they're caught...
6
by: cody | last post by:
Mostly always you use the using clause you deal with native ressources, so exception handlinjg is sooner or later inevitable. so why do we need to write try { using (File f =...
1
by: gemel | last post by:
I wish to implement error recovery using the Exception mechanism However, I'm not sure I fully appreciate some of the detail in which this works. The code that contains a potential error is...
1
by: Anonieko | last post by:
Understanding and Using Exceptions (this is a really long post...only read it if you (a) don't know what try/catch is OR (b) actually write catch(Exception ex) or catch{ }) The first thing I...
12
by: reycri | last post by:
While the following is allowed: if (a == b) SomeFunction(); else OtherFunction(); The following is not: try
3
by: Rahul Babbar | last post by:
Hi, I had the following doubts about the "For Read Only" clause. 1. How does a "for Read only" clause improve the performance? 2. How does a "for Read only" clause compare with "With UR"...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.