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

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,322 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!