Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C# C# Basics (Retired) Perfect Final

Emmanuel Oduja
Emmanuel Oduja
7,198 Points

Does not exist in the current context?? I don't understand where I went wrong.

I believe I am using the Try/Catch exceptions properly. Can anyone assist me with what the problem may be?

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
      static void Main()
        { 
            try
            {
            Console.Write("Enter the number of times to print \"Yay!\": ");
            string input = Console.ReadLine();
            counter = int.Parse(input);
            }
            catch(FormatException c)
            {
                Console.Write("You must enter a whole number.");
            }

            int i = 0;
            while(i < counter)
            {
                i += 1;   
                Console.WriteLine("Yay!");
            }
        }
    }
}

1 Answer

Steven Parker
Steven Parker
218,679 Points

It doesn't look like "counter" is ever declared.

Also, you probably don't want to encounter the loop after an exception occurs, so you might want to move it into the "try" block.