2

Why can't I assign object variables within the try block?

If I attempt to do this and clean up the variable in the finally block I get a compiler error: "use of unassigned local variable". This makes no sense because the variable is declared before the try block, and in the finally block I am first checking whether the variable is null.

Why can't the following code compile? I am checking whether dbc is null so there's no chance of it trying to do something with an unassigned variable.

eg:

DbConnection dbc;
try {
    dbc = <some method call returning an open DbConnection>
    // do stuff
} catch (Exception e) { // do stuff }
finally { 
    if (dbc != null) {
        dbc.Close();
    }
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
CJ7
  • 22,579
  • 65
  • 193
  • 321

2 Answers2

13

Change your declaration to DbConnection dbc = null; so the compiler can know for certain that the variable is assigned. (Merely declaring dbc is not the same as assigning it a value of null, you must be explicit with a local.)

The reason your existing code fails is that it is entirely possible for an exception to occur before dbc is set. As such, the compiler cannot assume that dbc is assigned by the time the finally block executes.

For more info, see section 5.3 of the language specification on definite assignment.

http://msdn.microsoft.com/en-us/library/aa691172(VS.71).aspx

Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
  • Surely the variable is null if it is not assigned? – CJ7 Aug 11 '10 at 03:11
  • @Craig, no, the variable is merely unassigned. `null` does not constitute the lack of assignment. – Anthony Pegram Aug 11 '10 at 03:12
  • Is this peculiar to C#? I don't remember this in any other language. – CJ7 Aug 11 '10 at 03:13
  • 1
    Global and member variables get auto-nulled, I believe. Local variables do not ... for efficiency purposes. Don't quote me on this one, look it up. – Hamish Grubijan Aug 11 '10 at 03:19
  • 1
    @Craig: Javascript behaves the same way: `null` and `undefined` [mean two slightly different things](http://joeyjavas.com/2007/04/25/javascript-difference-between-null-and-undefined/). – josh3736 Aug 11 '10 at 03:21
  • 1
    Just like VB.NET, C# also automatically nulls local variables. However, unlike VB.NET, the C# compiler thinks you probably made a mistake by having a path through your code that never initializes it. That catches a *lot* of bugs. – Hans Passant Aug 11 '10 at 03:35
  • In my case I return in the exception handler, so surely the compiler should know then that it's kind of irrelevant that it's unassigned as if it wasn't assigned the code referencing it will never be executed? – deed02392 Apr 03 '13 at 11:35
  • @deed02392, might need to see a code sample to be sure what you're talking about. But the general rule is that if you assign only in the `try`, code in a `catch`, `finally`, or afterwards cannot know for sure that the variable was assigned and it will be a compiler error to try to use it before an assignment occurs. – Anthony Pegram Apr 03 '13 at 14:14
  • Ah that's right, I had added conditions that didn't handle every exception so that some exceptions would proceed to let it be undefined. – deed02392 Apr 03 '13 at 14:21
1

Change this

DbConnection dbc;

to this

DbConnection dbc = null;
jwsample
  • 2,401
  • 19
  • 18