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

ultan gannon
ultan gannon
5,876 Points

Trying to use the if else to do format exceptions but it says there is something wrong with the var times

and something wrong with the ) brace too. No idea what I'm doing wrong here

Program.cs
using System;

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

            var index = -1;

            Console.Write("Enter the number of times to print \"Yay!\": ");

            string entry = Console.ReadLine();
            try
            {
                var times = int.Parse(entry);
                while (true)
            {

                var value = ++index;

                if (value == times)
                {
                    break;
                }



                Console.WriteLine("Yay!");
            }
            }
            catch (FormatException)

                if (var times <=0)
                {
                    Console.WriteLine("You must enter a positive number.");
                }
            else 
            {
                Console.WriteLine("You must enter a whole number.");
            }
        }
    }
}

1 Answer

Steven Parker
Steven Parker
231,248 Points

I see a few issues:

  • you define times inside the try block, it will not be accessible from outside it
  • the catch block is missing its braces ("{}")
  • in the if after catch you seem to be trying to define a new times but compare it at the same time
  • a negative number will not cause a format exception, it needs a different kind of test