Why are parameters needed for initialization inside try and catch sections?

I noticed that the C # compiler (.NET 4.5.2) does not allow me to compile the following code:

public void Test(out string value)
{
    //value = null;

    try
    {
        value = null;
    }
    catch (Exception ex)
    {
        //value = null;
    }
}

The following error failed:

The out value parameter must be assigned before the control removes the current method.

But if I uncomment the assignment in the section catch, it compiles successfully.
Obviously, it also compiles when I uncomment the assignment to the operator try.

So the question is: why is this not enough to initialize the parameter outinside the try block? . Why am I forced to initialize in a block catch?

+4
4

, , try , , , .

, out try, , - try , , , out, , , , , .

catch, ( ), , try- , , , .

+1

, out , . , , value = null;, catch, .

, if else, .

MSDN, out ref , , , , ref . , try catch, catch , , , ref.

, try, finally, , try , , out.

+6

out . try ( , ), -, . catch , / try { ... } catch{ ... }, finally { ... }

+3

# Sepcification,

, function member returns ( a return statement through execution reaching the end of the function member body). , - undefined , , , .

try..catch Try, catch. , , spec , output parameters .

- out parameter. Try , .

+3
source

Source: https://habr.com/ru/post/1605989/


All Articles