Whats the differenc of using a variable inside the try section and the catch section
string curNamespace;
try
{
curNamespace = "name"; // Works fine
}
catch (Exception e)
{
// Shows use of unassigned local variable
throw new Exception("Error reading " + curNamespace, e);
}
If i use variable inside try section it compiles fine, in catch section i get “Use of unassigned variable”
The compiler is complaining because you may encounter an exception before the value is initialized. Consider the following (very contrived) example:
The fix would be to initialize
curNamespaceto some default value outside thetry..catch. Have to wonder what you’re trying to use it for, though.