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

not sure how to tackle this problem after a few tries. where should the try and catch start within the code

the code is below and have to ensure whole numbers are used and anything else is an invalid entry.

I tried rewatching the video but i am stuck on the placement of the try and catch and what goes where in general.

below is how completed the 1st task without any issues.

appreciate any help

using System;

namespace Treehouse.CodeChallenges { class Program { static void Main() { Console.Write("Enter the number of times to print \"Yay!\": "); int counter = 0; String times = Console.ReadLine(); int totals = int.Parse(times); while (counter < totals) { Console.Write("\"Yay\"! "); counter = counter + 1; }

    }
}

}

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");
            int counter = 0;
            String times = Console.ReadLine();
            int totals = int.Parse(times);
            while (counter < totals)
            {
              Console.Write("\"Yay\"! ");
              counter = counter + 1;
            }  

        }
    }
}

must be very tired i managed to solve it :)

using System;

namespace Treehouse.CodeChallenges { class Program { static void Main() {

            try
            {
            Console.Write("Enter the number of times to print \"Yay!\": ");
                int counter = 0;
                String times = Console.ReadLine();
                int totals = int.Parse(times);
                while (counter < totals)
                     {
                     Console.Write("\"Yay\"! ");
                     counter = counter + 1;
                     } 
              }

        catch (FormatException)
                {
                Console.WriteLine("You must enter a whole number");
                }

    }
}

}

1 Answer

Steven Parker
Steven Parker
231,236 Points

Here's a few hints:

  • the line that has the int.parse will go inside the try block, since that's what you are testing
  • the lines that use the converted value can also go inside the try block
  • the catch block comes after the try block, and will contain the code to print the error message

Hopefully, that will get you going. Happy coding!   -sp:sparkles:

Steven Parker
Steven Parker
231,236 Points

I guess you fixed it while I was composing my answer. Congrats, and good job! :+1: