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 trialMatthew McPhail
2,173 PointsThis program works in the workspace, but does not work here. I'm not sure if I'm missing something
I get a compilation error with this code. This has me quite frustrated, I'm not sure if you had the wrong idea in mind when asking to use a try and catch.
using System;
namespace Treehouse.CodeChallenges
{
class Program
{
static void Main()
{
while(true)
{
Console.Write("Enter the number of times to print \"Yay!\": ");
var times = Console.ReadLine();
try
{
var repeat = int.Parse(times);
while(repeat > 0)
{
Console.WriteLine("Yay!");
repeat --;
}
}
catch(FormatException)
{
Console.WriteLine("You must enter a whole number.");
continue;
}
}
}
}
}
1 Answer
Steven Parker
231,248 PointsYou have an outer loop that prevents the program from ending.
A program built according to the instructions will prompt and accept input one time, and based on the input it will either print "Yay!"s or an error message. Either way, it should end. With the extra loop it never ends.
Matthew McPhail
2,173 PointsMatthew McPhail
2,173 PointsI'm not sure I get it though. The parse needs to be checked within the catch right? But we need the parse information to start a loop. And I think a catch needs to be tested within a loop from what I can gather... maybe I'm still a little confused.
Steven Parker
231,248 PointsSteven Parker
231,248 Points"try...catch" functionality is not related to loops. It can be used inside them or outside them as the conditions require.
And the parse needs to be done inside the try. So it makes sense to place the try (and the parse) before the loop (or around it).
Matthew McPhail
2,173 PointsMatthew McPhail
2,173 PointsI've figured it out, thanks for your help!