Try... Catch... Finally blocks

  • Thread starter Thread starter Grim Reaper
  • Start date Start date
Herfried,
Yes I agree, I think VB's method is "cleaner" & possibly more discoverable.


--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| > | BTW you don't have to indent each level
| > |
| > | using (SqlConnection connection = new ....)
| > | using (SqlCommand command = new ....)
| > | using (DataSet ds = new DataSet)
| > | {
| > | // blah
| > | }
| >
| > Ah! I was thinking there was a way...
|
| Mhm... I still think the way chosen in VB.NET is better than those of C#.
| The code above requires an additional indentation rule for the 'using'
| statement.
|
| --
| M S Herfried K. Wagner
| M V P <URL:http://dotnet.mvps.org/>
| V B <URL:http://classicvb.org/petition/>
|
 
Mattias said:
Tom,



Don't you have the same readability problem with nested try/catch
blocks?

Yes, but I would really have them...

SqlConnection connection = null;
SqlCommand command = null;
DataSet ds = null;

try
{
// blah
}
catch
{
// blah
}
finally
{
if (ds != null) ds.Dispose ();
if (command != null) command.Dispose ();
if (connection != null) connection.Dispose ();
}

Obviously contrived - but, I really don't nest try/catch blocks to much.
BTW you don't have to indent each level

I realize that - but, it's sort of an old habbit from perl (not leaving
dangling braces, I mean :) Still - I think the C# using statment would be
better off if it allowed multiple types in the list. Basically, they
should just be treated as IDisposable.
 
Cor said:
Tom,

A dataset has AFAIK not any unmanaged resource, so in my opinon the extra
disposing of that has not much sense.

A point that I find a problem with the "using" in this situations is that
you cannot catch any updating error, not even a concurrency error, which
makes that in my eyes than there is needed, that globaly should be tested
on a wide range of error situations.

Just my opinon.

Cor

Probably valid, Cor. I'm not much of a data guy... I simply used the
objects being referenced in the post I was responding to.
 
Tom,
Yes, but I would really have them...

SqlConnection connection = null;
SqlCommand command = null;
DataSet ds = null;

try
{
// blah
}
catch
{
// blah
}
finally
{
if (ds != null) ds.Dispose ();
if (command != null) command.Dispose ();
if (connection != null) connection.Dispose ();
}


OK but then the code is no longer equivalent to what you get with
using blocks. If ds.Dispose() throws an exception the command and
connection will be left undisposed if you don't have the nesting.


Mattias
 
Mattias said:
Tom,



OK but then the code is no longer equivalent to what you get with
using blocks. If ds.Dispose() throws an exception the command and
connection will be left undisposed if you don't have the nesting.


Mattias

Good point...
 
Ever wish you'd just kept quiet about something?? :D
_______________________________________
The Grim Reaper
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top