4

Sample code below gives an "Use of unassigned local variable 'resultCode'" when compiled:

    string answer;
    string resultCode;

    try
    {
        resultCode = "a"; 
    }
    catch
    {
        resultCode = "b";
    }
    finally
    {
        answer = resultCode;
    }

I would have thought the catch block above should catch all exceptions, and so that it was not possible for resultCode to be unassigned by the time the finally block is entered. Can anyone shed some light ? Thanks.

EDIT: Thanks all. This answer which quotes the documentation seems to answer it well: https://stackoverflow.com/a/8597901/70140

Community
  • 1
  • 1
Moe Sisko
  • 11,665
  • 8
  • 50
  • 80
  • 2
    What if `resultCode = "a";` throws an exception? I realise it won't, but the compiler doesn't know that. – DavidG Feb 17 '17 at 01:00
  • found some dupes: http://stackoverflow.com/questions/8597757/use-of-unassigned-local-variable-but-always-falls-into-assignment http://stackoverflow.com/questions/20521993/use-of-unassigned-local-variable-on-finally-block – Moe Sisko Feb 17 '17 at 01:38

4 Answers4

4

To illustrate:

string answer;
string resultCode;

try
{
    // anything here could go wrong
}
catch
{
    // anything here could go wrong
}
finally
{
    answer = resultCode;
}

The compiler can't assume or guarantee at this point that resultCode was ever assigned a value. So it warns you that there's a potential use of an unassigned variable.

David
  • 208,112
  • 36
  • 198
  • 279
  • 1
    +1 but I think it's worth adding that if either the `finally` block or all of the `try` and `catch` blocks definitely assign a value, then it it is definitely defined after the entire construct. – Jon Hanna Feb 17 '17 at 01:15
1

Add some explanation, For example, in the following code, the variable n is initialized inside the try block. An attempt to use this variable outside the try block in the Write(n) statement will generate a compiler error.

int n;  
try   
{  
    int a = 0; // maybe a throw will happen here and the variable n will not initialized
    // Do not initialize this variable here.  
    n = 123;  
}  
catch  
{  
}  
// Error: Use of unassigned local variable 'n'.  
Console.Write(n);  

As suggested in the comments if you assign also in the Try and in the Catch like this, adn try to assign after the blocks

 string answer;
 string resultCode;

 try
 {
    resultCode = "a";
 }
 catch
 {
    resultCode = "b";
 }
 finally
 {
     // answer = resultCode;
 }
 answer = resultCode;

It will compile.

Moshe D
  • 768
  • 1
  • 4
  • 13
0

The compiler cannot guarantee that any code inside the try or catch blocks will actually run without an exception happening. Which leaves, in theory, the value of resultCode as unassigned when you try to use it.

DavidG
  • 113,891
  • 12
  • 217
  • 223
0

Visual Studio does not know you are assigning 'resultCode' a value. You need to give it a value before hand. Example code at bottom.

This of this as a hierarchy. Visual Studio does not see the definitions of 'resultCode' in the try/catch.

string answer = "";
string resultCode = "";

try
{
    resultCode = "a"; 
}
catch
{
    resultCode = "b";
}
finally
{
    answer = resultCode;
}
Noah Heber
  • 82
  • 1
  • 12
  • It's not, but no matter what happens, it will be defined in the try/catch. The compiler just does not know that. – Noah Heber Feb 17 '17 at 01:23