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

Sean Flanagan
Sean Flanagan
33,234 Points

C# Basics Final Task 2

Hi. I'm stuck on this one.

using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");
            int counter = 0;
            String times = Console.ReadLine();
            int totals = int.Parse(times);
            while (counter < totals)
            {
              Console.Write("\"Yay!\"! ");
              counter += 1;
            }
            try
            {

            }
            catch
            {

            }
        }
    }
}

I've tried to throw a Format Exception.

I've also noticed that despite ticking the "Attach my code to this post" tickbox, other users still ask to see my syntax, so I wonder if there's a technical fault here. I'd appreciate any help.

2 Answers

Stéphane Diez
Stéphane Diez
19,350 Points

It should look something like this:

using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {

                Console.Write("Enter the number of times to print \"Yay!\": ");

                var entry = Console.ReadLine();
                try
                {

                    var NumberOfTimes = int.Parse(entry);

                   while(NumberOfTimes>0)
                   {
                        Console.WriteLine("Yay!");
                        NumberOfTimes--;
                   }

                }

                catch (FormatException)//ArgumentNullException
                {
                    Console.WriteLine("You must enter a whole number.");
                }
        }
    }
}

You want the try to be before you make the parsing. Then the while must be inside the try aswell. In the catch you want to put FormatException inside the (). You can check the c# documentation if you need some more input, or try to review the actual video again.

Sean Flanagan
Sean Flanagan
33,234 Points

Hi Stephane. It's worked. I've given you an up vote and Best Answer. Thanks. :)