Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Basics (Retired) Perfect Variable Scope

Alex Bonilla
Alex Bonilla
17,765 Points

I don't understand where to insert the try catch.

I re watched the video and thought I should enter it Try and catch near the top but the compiler threw more errors. I put it this way not to change the scope like the question says. Anyone have any suggestions?

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        { 
            try{
                int input = int.Parse(entry);
            }
            catch (FormatException){
                 input = Console.ReadLine();
            }


            if (input == "quit")
            {
                string output = "Goodbye.";
            }
            else
            {
                string output = "You entered " + input + ".";
            }

            Console.WriteLine(output);
        }
    }
}

2 Answers

Steven Parker
Steven Parker
231,269 Points

I'm not sure where you got the idea that you should add a "try...catch", or for that matter, any additional functional code to the original. You're only supposed to fix variable scopes so that the code compiles correctly.

The instructions say specifically, "Be sure to not change the intent or intended behavior of the code."

You don't need an input validation(try/catch). Just declare the input and output variables as a string.