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

exception Handling. Its failing in the else

using System;

namespace Treehouse.CodeChallenges { class Program { static void Main() { Console.Write("Enter the number of times to print \"Yay!\": "); string entry=Console.ReadLine(); //Store the number of time in a variable and parse it try { int n= int.Parse(entry);

         if (n<=0)
            { 
           Console.WriteLine("It has to be a positive number"); 
           continue;
            }

           int i=1;

         else
          {

                while (i<=n)
                  {

                    Console.WriteLine("Yay!");
                    i=i+1;
                  }
          }
       }

              catch(FormatException)
              {
                  Console.WriteLine("You must enter a Whole Number");
                continue;

              }


    }
}

}

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");
            string entry=Console.ReadLine();
        //Store the number of time in a variable and parse it
         try
         {
               int n= int.Parse(entry);

             if (n<=0)
                { 
               Console.WriteLine("It has to be a positive number"); 
               continue;
                }

               int i=1;

             else
              {

                    while (i<=n)
                      {

                        Console.WriteLine("Yay!");
                        i=i+1;
                      }
              }
           }

                  catch(FormatException)
                  {
                      Console.WriteLine("You must enter a Whole Number");
                    continue;

                  }


        }
    }
}

1 Answer

Damien Watson
Damien Watson
27,419 Points

Hi Amit,

It is because your 'int i=1;' is in the wrong place:

using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");
            string entry = Console.ReadLine();
            try {
                int num = int.Parse(entry);
                if (num<=0) { 
                   Console.WriteLine("It has to be a positive number");
                } else {
                    int i = 1;
                    while (i<=num)
                    {
                        Console.WriteLine("Yay!");
                        i=i+1;
                    }
                    /* // Or use a for loop 
                    for (int i=0; i<num; i++) {
                        Console.WriteLine("Yay!");
                    }
                    */
                }
            } catch(Exception) {
                Console.WriteLine("You must enter a Whole Number");   
            }
        }
    }
}