Re: Try Catch Finally , pointers and Vb.net 2005 compiler warnings
- From: "Herfried K. Wagner [MVP]" <hirf-spam-me-here@xxxxxx>
- Date: Wed, 26 Oct 2005 15:24:24 +0200
"David Browne" <davidbaxterbrowne no potted meat@xxxxxxxxxxx> schrieb:
As the Using statement does your Try/Finally with a check for not Nothing as
Herfried shows.
I still think that the compiler is stupid.
\\\ Dim f As Form Try f = New Form() Catch ex As Exception
' Warning 1: Variable 'f' is used before it has been assigned a ' value. A null reference exception could result at runtime. If f IsNot Nothing Then f.Dispose() End If End Try ///
The 'f' in front of 'IsNot' is underlined and the warning included as a comment in the snippet above is shown. However, it's impossible that a 'NullReferenceException' can be thrown at this line.
I am not specialized in what a compiler is able to detect, but I think that this warning is counterproductive. It leads people to write explicit assignments ('Dim f As Form = Nothing'), which is not necessary because the compiler adds the assignment automatically. If there was no 'Using' statement in VB 2005, the code above is IMO pretty "good practice" and there it should not cause any warnings.
No. The above code is and always was bad practice. Declaring local variables without assigning them immediately is rarely the right thing to do. Here the code should be:
I have to disagree. Imagine we are not using a simple form but we are using a database connection object. Creating the object could throw an exception. In this case an additional 'Try...Catch' block + conditional execution of the following code would be required, which increases complexity.
No. The catch block should rethrow the exception. That _is_ conditional execution of the following code.
This strongly depends on the scenario.
But if creating the object throws an exception you don't have to dispose it.
That's true. However, the construct I showed was not intended to be a replacement for simple cases of 'Using' which are dealing with a single object only. My main concern is about the uselessness of the compiler warning.
The object creation belongs in a higher scope than its use and Dispose.
I believe it belongs to the same scope if possible. Higher scopes are possible, but not always the best solution (minimum scope is often the best choice, and in this particular case it's the same scope).
The main question is -- and this question has been discussed hundreds of times -- whether or not the 'Try', 'Catch', and 'Finally' branches should share the same scope.
But big picture is that the using block should be used at every assignment of a local variable to a Disposable type.
The 'Using' block has IMO one big disadvantage: It introduces an additional scope, although the 'Dispose' pattern has nothing to do with scopes.
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
.
- References:
- Try Catch Finally , pointers and Vb.net 2005 compiler warnings
- From: Bob
- Re: Try Catch Finally , pointers and Vb.net 2005 compiler warnings
- From: Herfried K. Wagner [MVP]
- Re: Try Catch Finally , pointers and Vb.net 2005 compiler warnings
- From: David Browne
- Try Catch Finally , pointers and Vb.net 2005 compiler warnings
- Prev by Date: RE: How to have .NET control ignore mouse events (or forward, or bubbl
- Next by Date: Re: Control size/location of external app?
- Previous by thread: Re: Try Catch Finally , pointers and Vb.net 2005 compiler warnings
- Next by thread: Help with Repeater Control
- Index(es):
Relevant Pages
|