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 Wrap Up

Tony Lawrence
Tony Lawrence
3,056 Points

Trouble with the Challenge identifying catch statement.

Sorry to intrude again. I'm having trouble figuring out where to put the catch method in order to make format exceptions. Though I'm concerned that the challenge will have trouble with me changing the code to where it states that I'm failing to the first challenge. I'm getting errors stating that there is no enclosing loop to break or continue or it fails to recognize my catch statement.

This what I have so far:

using System;

namespace Treehouse.CodeChallenges { class Program { static void Main() { Console.Write("Enter the number of times to print \"Yay!\": ");

        string input = Console.ReadLine();

        var redo = int.Parse(input);

        try {

            while (redo != 0) 
            {
                Console.WriteLine("Yay!");
                redo -= 1;
            }
            if (redo == 0)
            {
                break;
            }


        }

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

                     }


    }
}

}

I'm wondering if I now have to reformat to make the while loop be (true) or not.

1 Answer

Steven Parker
Steven Parker
231,275 Points

Be careful about where you put your try block.

In particular, you'll want the line with the int.Parse to go inside the try block so it can detect any problems converting the value.

Also, people often over-think this one and get too creative, as with any challenge it's good to keep things simple. Here are a few hints for success:

  • Your program must ask for and accept input ONE TIME ONLY
  • 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. There are other perfectly good methods that can be used for validation, but they will not pass this challenge