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

My C# catch statement is causing me some errors.

In my C# program, my catch statement and everything below it is creating an error. I've been online to MSDN.microsoft.com to see if I'm using the try/catch correctly and it seems I am but I'm still getting the errors.

Any help I could get would be greatly appreciated!

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
        int counter = 0;  
          while (true)
          {    
                 Console.Write("Enter the number of times to print \"Yay!\": ");
              try
              {    
                 var entry = Console.ReadLine();
                 int userInput = int.Parse(entry);
                 // userInput can only be an integer, not a decimal
                   while (userInput > counter)
                   {
                   Console.WriteLine("Yay!");
                   counter = counter + 1;  
                   }
               catch(FormatException)
               {
               Console.WriteLine("You must enter a whole number.");
               }  //catch    
               continue;
             } //try     
             Console.Writeline("Goodbye");
          }  //while  
       } //static   
    }  //class
Benjamin Lange
Benjamin Lange
16,178 Points

It looks like you're missing the closing curly brace for the try block. There should be another } after the while loop. Also, capitalize the L in WriteLine in the last line. You also probably want to add a break; statement right after the while loop closes. Otherwise, the program will loop forever.

1 Answer

Steven Parker
Steven Parker
231,269 Points

It looks like you're missing a closing brace for the try block (right before the catch), and you spelled WriteLine with a lower-case "L" in one place.

:point_right: But even with the errors fixed, the program deviates from the instructions.

This has been a topic of many previous questions, but I'll repost the info here:

People often over-think this one and get too creative, and their answer is rejected for a variety of reasons (some which don't seem to make any sense).

Here are the secret "rules" you must follow for success on this one:

  • 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