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. :)