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 trialJoe Castleton
1,121 PointsCan anyone explain why this try and catch method isn't working?
Hi,
This is for the final part of the C# basics class. I was just wondering why this try and catch method isn't working? I have checked I am using the write error code and I am still struggling to understand what I can do to fix this?
Any help would be appreciated :)
using System;
namespace Treehouse.CodeChallenges
{
class Program
{
static void Main()
{
// Get number of Yays to print
Console.Write("Enter the number of times to print \"Yay!\": ");
var entry = Console.ReadLine();
// Recognise number of Yays as an integer
var numberYay = int.Parse(entry);
try
{
// Print number of Yays
while(numberYay > 0)
{
Console.Write("Yay!");
numberYay -= 1;
}
}
catch(System.FormatException)
{
Console.WriteLine("That is not valid input.");
}
}
}
}
1 Answer
Jon Wood
9,884 PointsThe try/catch
is for the int.Parse
method. Put that into the try
block, and you should be good to go!
Joe Castleton
1,121 PointsJoe Castleton
1,121 PointsThat's sorted it, thank you!
Jon Wood
9,884 PointsJon Wood
9,884 PointsAwesome! Glad it helped!