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

bosco dsa
bosco dsa
12,599 Points

Having trouble with catching the exception on the final challenge

It says "Bummer. Try again." When I run it in workspaces it works perfectly.

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            while (true) {
                try {
                    var doIt=Int32.Parse(Console.ReadLine());
                    Console.Write("Enter the number of times to print \"Yay!\": ");
                    while (doIt!=0) {
                        Console.WriteLine("Yay!");
                        doIt-=1;
                    }
                }
                catch (SystemException) {
                    Console.WriteLine("You must enter a whole number.");
                    continue;
                }

                break;    
            }
        }
    }
}

1 Answer

Hi Bosco,

For this challenge you don't need a loop. So you can take out the while, break, and continue statements. The program should just end if they don't enter a number rather than continually prompt for a number.

The other issue is that you should be a catching a FormatException

I'm not sure if it will affect the challenge but the ReadLine should be after the Write so that the user knows what they're supposed to enter.