Re: Try Catch Finally , pointers and Vb.net 2005 compiler warnings



"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/>


.



Relevant Pages

  • Re: proc A def/calls proc B: variable scoping rules.
    ... the exception occurs. ... In this case the assignment is never reached.... ... def A: ... effects the entire scope. ...
    (comp.lang.python)
  • Re: C# Language Proposal for out Parameters
    ... >> If the assignment isn't made because an exception has been thrown, ... inout semantics, not to mention reducing overall clarity of the code. ... The fundamental problem is that GetNewValue would ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Controlled types and exception safety
    ... >> propagate an exception. ... >> For an Adjust invoked as part of an assignment operation, ... But a user-defined constructor is ... a user-defined constructor has just turned on the ...
    (comp.lang.ada)
  • Re: struct problems
    ... because my previous programming experience made the assignment before ... at least three programming languages by copy-pasteing. ... that you are ONLY in the scope of functions. ... GNU C and disregard the limitations of other compilers. ...
    (comp.lang.c)
  • RE: Another useful error message - not
    ... Inner exception: Object reference not set to an instance of an object. ... I am checking a condition on promoted field in the orchestration. ... The Ack received is set to a message in an outer scope. ...
    (microsoft.public.biztalk.general)