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

christopher simons
christopher simons
1,651 Points

How to I get this catch to work?

I seem to be getting the catch wrong.

Question 1: The " var entry = int.Parse(Console.ReadLine()); " is retrieving the user input (string) and converting/storing it in entry right?

The "try" is telling the program to try the line of code within {} I believe. Within the try is a while loop which works perfect so long as the user input is an int.

When anything other than an int is the user input, I get an exception.

I was thinking "catch(FormatException)" would catch the exception and ignore it.

What am I doing wrong with this thing. I seem to be struggling to hard with this.

Program.cs
using System;

namespace Treehouse.CodeChallenges{
    class Program{
        static void Main(){
          int counter = 0;
          Console.Write("Enter the number of times to print \"Yay!\": ");
          var entry = int.Parse(Console.ReadLine());
                  try{
                    while(counter != entry)
                      {Console.Write("Yay"); counter = counter + 1;} 
                    }
                    catch(FormatException){
                      Console.WriteLine("That is not valid input.");
                    }    
        }
    }
}

2 Answers

Try putting "var entry = int.Parse(Console.ReadLine());" inside the try block.

Example:

try
{
  var entry = int.Parse(Console.ReadLine());
}
catch(FormatException)
{
  // Handle the exception.
}
christopher simons
christopher simons
1,651 Points

Thanks Chris! I had to step away from the computer for a minute so my brain would work right I guess.

christopher simons
christopher simons
1,651 Points

using System;

namespace Treehouse.CodeChallenges { class Program { static void Main() { try { int counter = 0; Console.Write("Enter the number of times to print \"Yay!\": "); var entry = int.Parse(Console.ReadLine()); if(entry <= 0) { Console.WriteLine("You must enter a positive number.");

                    }
                    else
                    {
                          while(counter != entry)
                        {                      
                          Console.Write("Yay ");
                          counter = counter + 1;
                        }
                    }
              }
              catch(FormatException)
              {
                Console.WriteLine("You must enter a whole number.");
              }
    }
}

}