5

having this code, I don't understand why if assigning a variable in a finally block doesn't understand it will ALWAYS be assigned. I think I missing a valid option where currency won't be assigned. If you know, will be great to understand why. much appreciate it!

Thanks!

CurrencyVO currency;

try
{
     if (idConnection.HasValue && idConnection != 0)
     {
         currencyConnection = client.GetConnection(idConnection.Value);
         model.Connection = currencyConnection;
     }
     else 
     {
         int providerUserKey = (int)Models.UserModel.GetUser().ProviderUserKey;
         currencyConnection = client.GetConnection(providerUserKey);
     }                        
     currency = model.Currencies.SingleOrDefault(c => c.IdCountry == currencyConnection.idcountry) ?? new CurrencyVO();    
} 
catch
{
      currency = new CurrencyVO();                    
} 
finally
{
      model.PublishedContainer.Currency = currency;
}

the error happens on the finally block. If i take it out of the finally block like this :

                } catch {
                    currency = new CurrencyVO();
                }
                model.PublishedContainer.Currency = currency;

it works fine.

Glory Raj
  • 17,397
  • 27
  • 100
  • 203
Bart Calixto
  • 19,210
  • 11
  • 78
  • 114

4 Answers4

8

The definite assignment tracking that the C# compiler performs doesn't necessarily perform a complete analysis (that wouldn't be possible in the general case) - there are rules that restrict how complex of an analysis the compiler will perform. The rule covering the finally block here is documented at http://msdn.microsoft.com/en-us/library/aa691181.aspx:

For a try statement stmt of the form:

try try-block finally finally-block

  • The definite assignment state of v at the beginning of try-block is the same as the definite assignment state of v at the beginning of stmt.
  • The definite assignment state of v at the beginning of finally-block is the same as the definite assignment state of v at the beginning of stmt.
  • ...

So for your particular example, since currency is not definitely assigned at the beginning of the try block, it is considered to be not definitely assigned at the beginning of the finally block.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • 1
    You beat me while I was testing the code :) In the above example, if the constructor for CurrencyVO threw an exception, execution would hit the finally block with currency still undefined. – TheEvilPenguin Dec 21 '11 at 23:59
1

What if new CurrencyVO() causes an exception in the catch block? A-ha!

Scott Rippey
  • 15,614
  • 5
  • 70
  • 85
1

You can reason it out without hitting the language spec. The failure mode here is that the statement in the catch block can throw an exception. Quite possible, throwing an exception in a constructor is supported in C#. But true in general, any statement can throw an exception. Which will leave the variable uninitialized. The assignment in the finally block will always execute. And use an uninitialized variable in that case.

You'll need to think about what you to have happen in that case, if it is at all relevant. But the obvious workaround is to simply initialize the variable to null explicitly.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
0

You are creating a new CurrencyVO object inside the catch block: which will only execute if there is an error/exception. Therefore, if you encounter no exception: the currency variable will not get assigned. That's why you can't use it.

edit: make the following changes for your code to compile:

CurrencyVO currency = null;

and in finally:

if (currency != null)
   model.PublishedContainer.Currency = currency;

http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/a994a0ff-432b-4d23-b7d2-838b0b961de0

olegvs
  • 345
  • 3
  • 11
  • Currency is assigned in the last line of the try –  Dec 21 '11 at 23:47
  • @Joe: true, I noticed that later but I guess that compiler cannot guarantee that that assignment will be executed and that's why it doesn't allow use of the variable – olegvs Dec 21 '11 at 23:59
  • Really? It seems like it will be assigned in the try or catch. One has to be caught, right? –  Dec 22 '11 at 00:08