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 trialRaiyan Rahman
Courses Plus Student 8,091 PointsTry/Catch block isn't being evaluated.
I implemented the try/catch block as requested but the program isn't able to evaluate it.
Is there a way to fix this?
int value = int.Parse(Console.ReadLine());
if (value < 0 || value > 20)
{
throw new System.Exception();
}
try
{
Console.WriteLine(string.Format("You entered {0}",value));
}
catch(Exception)
{
System.Console.WriteLine("Value is out of bounds!");
}
2 Answers
andren
28,558 PointsYour Try/Catch code is correct, but you have wrapped it around the wrong code. The challenge asks you to:
wrap the testing logic with a try/catch
The "testing logic" is the if statement that verifies that the value passed in is correct.
If you simply take your Try/Catch block and wrap it arund the if statement instead of the print statement like this:
int value = int.Parse(Console.ReadLine());
try
{
if (value < 0 || value > 20)
{
throw new System.Exception();
}
}
catch(Exception)
{
System.Console.WriteLine("Value is out of bounds!");
}
Console.WriteLine(string.Format("You entered {0}",value));
Then your code will pass.
Raiyan Rahman
Courses Plus Student 8,091 PointsThanks for the advice everyone; I was able to clear this challenge.
Edward Ries
7,388 PointsEdward Ries
7,388 PointsIf you haven't brought in System with a using System; statement at the top then your problem is Console.WriteLine is missing System.Console.WriteLine. Also, make sure there isn't any punctuation missing. I don't remember if there was a requirement for a period or exclamation mark at the end but you have one on the exception but not for the try block.