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

Needing help again....

I feel somewhat dumb for having to ask for help like 3 times today but this will be the last. I've used while, try and catch and still don't know if that is even what I am meant to be doing

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            while(true)
            {
            try
            {
             Console.Write("Enter the number of times to print \"Yay!\": ");
             string input = Console.ReadLine();

             int count = int.Parse(input);

            int i = 0;
             while(i < count)
             {
                i += 1;   
                Console.WriteLine("Yay!");
             }
             catch (FormatException)
             {
                Console.WriteLine("You must enter a whole number");
                continue;
             }
             }
        }
    }
}

1 Answer

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,320 Points

Try take it step by step:

  • Wrap all of the code contained in the Main method in a try/catch block
try
{
    // code goes here
}
catch () // The catch block should catch FormatException exceptions
{
    //Inside of the catch block, output to the console the message "You must enter a whole number."
}

Looks like your catch block is inside your try block - maybe try rewatch the video about try/catch if you have problems understanding how it's working :-)

Also, the try/catch should not be wrapped in a while-loop :-) Just take the original code and put it inside the try block :-)

Anyway, I hope this helps :-)

It's certainly much more understandable than how it's said haha, thanks!!

EDIT

I just tried that and now it is not in the try block but it's still shooting out errors..

ERRORS:

Program.cs(20,12): error CS1525: Unexpected symbol `catch'

Program.cs(21,12): error CS1525: Unexpected symbol `{'

Program.cs(25,4): error CS1524: Unexpected symbol }', expectingcatch' or `finally'

Program.cs(26,246): error CS1525: Unexpected symbol end-of-file', expectingcatch' or `finally'

Program.cs(27,1): error CS1514: Unexpected symbol end-of-file', expecting.' or `{'

Compilation failed: 5 error(s), 0 warnings

EDIT

Finally worked, yay!