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 trialRino Sincic
467 PointsTry and catch
I get an error asking me if i cought FormatException thrown by int.Parse using try and catch? can you look at my code and help me i don't understand what the problem is.. Thanks
using System;
namespace Treehouse.CodeChallenges
{
class Program
{
static void Main()
{
Console.Write("Enter the number of times to print \"Yay!\": ");
string input = Console.ReadLine();
int times = 0;
int broj = int.Parse(input);
try
{
broj = int.Parse(input);
}
catch(FormatException)
{
Console.Write("You must enter a whole number.");
}
while (times < broj)
{
times += 1;
Console.Write("Yay!");
}
}
}
}
1 Answer
Justin Kraft
26,327 PointsSimply declare the variable broj
int broj;
Then use
broj = int.Parse(input);