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 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
231,269 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.