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 trialChaltu Oli
4,915 PointsI don't understand the error?
I don't know what the error is
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 index = int.Parse(entry);
int runningTime = 1;
while(index >= runningTime){
Console.WriteLine("Yay!");
index--;
}}catch(FormatException){
Console.WriteLine("Invalid answer try agian.");
continue;
}
}
}
}
2 Answers
Balazs Peak
46,160 PointsFor some reason, you've put that "continue;" into the catch phrase block. That is unnecessary. Just delete it and pass the challange. I hope this helps! Much love!
Scott Wyngarden
Courses Plus Student 16,700 PointsHere's your code formatted so that the error is easier to see:
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 index = int.Parse(entry);
int runningTime = 1;
while (index >= runningTime)
{
Console.WriteLine("Yay!");
index--;
}
}
catch (FormatException)
{
Console.WriteLine("Invalid answer try agian.");
continue;
}
}
}
}
When I put that code in an IDE, the complaint was that you're trying to use continue
outside of a loop, which is pretty easy to see when the code looks like this. Both break
and continue
are only usable when they're placed inside of the body of a loop (for, foreach, while, do while, etc), but since the catch
block of the try/catch is outside the body of the loop, the code won't compile.
I'd also suggest moving the while loop you have outside of the try/catch block (which might require you to move some of your variable declarations), since running the loop is somewhat of a separate task as getting and validating the input from the user. Doing that might make it clearer what you expect to sometimes throw a Format Exception, and makes the logical flow of your code easier to follow.