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

Leo Drakenberg
Leo Drakenberg
16,911 Points

C3 Final Try Catch

Can't be able to get this to work. What is going wrong here?

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");
            var times = int.Parse(Console.ReadLine());

            while (times > 0)
            {
              try
              {



              if (times > 0)
              {
                 Console.Write("Yay!");
                 times -= 1;
                 continue;
              }
             }
              catch (FormatException)
              {
                Console.WriteLine("You must enter a whole number.");
                continue;
              }
            }

        }
    }
}
Leo Drakenberg
Leo Drakenberg
16,911 Points

The "Bummer" I get:

Did you catch FormatException thrown by int.Parse using try and catch?

1 Answer

Steven Parker
Steven Parker
221,292 Points

I guess you didn't find any of the previous posts where I answered this helpful, like this one.

Anyway, here are some hints:

  • Your program must ask for and accept input ONE TIME ONLY (input will not be in a loop)
  • If the input validates, your program will perform exactly as in Task 1
  • If the input does not validate, it will print the error message and exit
  • Validation must be done by exception catching. This means your Parse must be inside a try block.
  • The loop used on success for printing will be after the catch block.