3

I am trying to figure out why this code is able to use variable from try scope. If i didn't implement return to catch{} it would cause error, but with return in catch it all runs without problem, I really don't get why, I would expect that both will cause error. So Why it is able to run?

static void Main(string[] args)
    {
        DayOfWeek favDay;
        try
        {
            favDay = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), Console.ReadLine());
        }
        catch(Exception x)
        {
            Console.WriteLine(x.Message);
            Console.ReadLine();
            return; // Without implementing this return I cannot use variable favDay after in Main to pass it into Method.
        }
        Print(favDay);
        Console.ReadLine();
    }

static void Print(DayOfWeek x)
    {
        switch (x)
        {
            case DayOfWeek.Friday:
                Console.WriteLine("Weieeeee");
                break;
            default:
                Console.WriteLine(":(");
                break;
        }
    }

Thank you for your response. :)

Jakub Gause
  • 309
  • 3
  • 10

2 Answers2

3

By adding the return; statement, you prevent the method from being able to reach the use of the variable favDay except when it's been assigned. The only other code path that would lead to the use, at the end of the method, goes through the successful assignment in the try block, so the variable is assigned before the later use.

Without the return; statement, you would get the "use of unassigned local variable" error message, because execution can reach the catch block before assigning the variable in the try block, and then fall through to the use of the variable, without having assigned a value to the variable.

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
  • Oh, nice Thank you so much for explanation, So just for test I tried update my code with something else so I test if I get it right, and it all runs smoothly. Just to be sure: I just added goto Finish; instead of return and place Finish after line where variable was used. (I know goto is bad, but just to test my new knowledge) Did I get it right? – Jakub Gause Apr 17 '17 at 08:15
  • @Jakub: yes, your `goto` would be equivalent to a `return` statement, at least in the respect that it would cause execution to bypass the use of the variable. The compiler is able to analyze the method's code execution paths well enough to still avoid the error. Do note, however, that the rules for "definite assignment" (you can look that up) are relatively narrow. It is possible to create code examples where a human can logically reason that there's no use of an unassigned variable, but the compiler can't. Searching Stack Overflow for that error message will show you a number of such examples – Peter Duniho Apr 17 '17 at 08:25
0

The cause of the act you are seeing there is due to the fact that the variable have not been initialized and return; in catch will automatically infer the empty value for the variable initialization required to use the variable for other funcs. So the method will be an Error at that point to save any exceptionable situation further.

Apurv Mahesh
  • 129
  • 4
  • No, return in catch block will exit the method before any other code in that method will try to use the unassigned variable – Sir Rufo Apr 17 '17 at 08:05
  • @SirRufo I also meant the same thing bro... Sorry for not explaining it completely. And thanks... have edited the answer. – Apurv Mahesh Apr 17 '17 at 09:26