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

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