Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question about "using" and Exceptions

Status
Not open for further replies.

anonim1

Programmer
Dec 10, 2004
108
US
I have the following bit of code:

using(DataSet ds = db.ExecuteDataSet(cmd)) {
// Return the DataTable
return ds.Tables[0];
}

I know that creating an instance in a using statement will ensure that Dispose is called on the object when the using statement is exited. However, I'm confused as to where I can implement a try/catch block here.

Suppose that the network link goes down between the web server and the database server. I've tested this, and it throws a SQLException. However, I am not sure where I can include a try/catch block here. Would it go inside the parentheses of the using statement, or in the body of it?
 
I don't belive you need to use Using in this instance. It is used for unmanged code. See the help on Using and Garbage collection. Here are some quotes from the VS help:

When you write code that uses an object that encapsulates a resource, you should make sure that the object's Dispose method gets called when you are finished using the object. You can do this with the C# using statement or by implementing a try/finally block in other languages that target the common language runtime.

C# Using Statement
The C# programming language's using statement makes a call to the Dispose method more automatic, by simplifying the code that you must write to create and clean up an object. The using statement obtains one or more resources, executes the statements that you specify, and then disposes of the object. Note that the using statement is only useful for objects with a lifetime that does not extend beyond the method in which the objects are constructed. The following code example creates and cleans up an instance of the ResourceWrapper class, as illustrated in the C# example of implementing a Dispose method.

The .NET Framework's garbage collector manages the allocation and release of memory for your application. Each time you use the new operator to create an object, the runtime allocates memory for the object from the managed heap. As long as address space is available in the managed heap, the runtime continues to allocate space for new objects. However, memory is not infinite. Eventually the garbage collector must perform a collection in order to free some memory. The garbage collector's optimizing engine determines the best time to perform a collection, based upon the allocations being made. When the garbage collector performs a collection, it checks for objects in the managed heap that are no longer being used by the application and performs the necessary operations to reclaim their memory.

Jim
 
Jim,

Thank you, you are absolutely right. The using statement is a good way to automatically close, for example, a SqlDataReader object. However, the DataSet object does not implement the IDisposable interface; therefore, it does not make sense to instantiate it inside a using statement.

So to answer my own question, I would want to do something like this:

<...snip...>
try {
DataSet ds = db.ExecuteDataSet(cmd);
}
catch(SqlException e) {
// handle error
}
return ds.Tables[0];

Thanks for pointing this out!
 
Not a problem. I had never seen that code, since i use VB, so I learned something new. :)

Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top