Re: Problems Handling Errors Correctly
- From: Peter Duniho <NpOeStPeAdM@xxxxxxxxxxxxxxxx>
- Date: Sat, 15 Sep 2007 14:58:53 -0700
Jonathan Wood wrote:
Actually, it is simple.
Well now, I do recall how hard the .NET developers tried to find a solution to the lack of deterministic finalization, so I don't quite agree that "it is simple" tells the entire story.
I'm referring to the statement in your first that says that the question of handling errors is "rarely that simple". Certainly there are things about finalization that can complicate things, but these generally only come up if you don't write the code correct. If you handle exceptions as you should and ensure that objects are disposed when they should be, it really is "that simple".
[...]If you prefer, you could do this instead, which is equivalent to the above:
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(),
StreamReader reader = new StreamReader(webResponse.GetResponseStream()))
{
// code here
}
I do prefer, but this gives me the error: Cannot use more than one type in a for, using, fixed, or declaration statement.
Sorry...my mistake. Now that I think about it, I believe that the variable declaration inside the "using" statement has to be the same syntax as a legal declaration anywhere else, while at the same time semi-colons are not allowed (ie, it has to be a single statement). This means that you can only declare objects of a single type for any one using statement.
So you'll have to go with the nested using statements instead. Hopefully not a big deal.
[...]For that matter, how about webRequest?
No.
Because it doesn't implement IDisposable?
Exactly.
Yes. If the class implements IDisposable, then you need the "using" statement. If it doesn't, you don't.
And what's the quickest way to find out? If it has a Dispose() method?
Look at the documentation. Every .NET class has on the first doc page for that class a complete description of all classes it inherits and interfaces it implements. If the class implements IDisposable, it will say so there.
Alternatively, you could always just look for the Dispose() or IDisposable.Dispose() interface in the class methods.
Also, can you tell me if the using statement eliminates the need to call the Close() method on these objects?
Generally speaking it should.
However, I can't rule out the possibility of a class that requires explicit closing for best results. A Close() method may have other semantics than simply disposing; I don't think that a class is required to perform a Close() per se in its Dispose() method, so for example if Close() flushes some buffers implicitly, you may not have a guarantee that Dispose() would also do so (even though in many classes it very well might).
Personally, I would go ahead and Close() as well. I treat Close() and similar methods as distinct from Dispose(), even where they appear to duplicate behavior.
Pete
.
- Follow-Ups:
- Re: Problems Handling Errors Correctly
- From: Jonathan Wood
- Re: Problems Handling Errors Correctly
- References:
- Problems Handling Errors Correctly
- From: Jonathan Wood
- Re: Problems Handling Errors Correctly
- From: Peter Duniho
- Re: Problems Handling Errors Correctly
- From: Jonathan Wood
- Problems Handling Errors Correctly
- Prev by Date: Re: All Possible Combinations of Multiple Lists
- Next by Date: dates
- Previous by thread: Re: Problems Handling Errors Correctly
- Next by thread: Re: Problems Handling Errors Correctly
- Index(es):
Relevant Pages
|
Loading