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 trialAmar Resic
2,922 PointsHelp with coding challenge try, catch
Need some help integrating the conditional if statement into a try, catch statement. Keeps giving me the error "I entered "25". I expected "Value is out of bounds!" but got "You entered 25" instead". . I've tried many variations of this and just not getting it. The instructions are not clear to me if they still need the conditional included in the answer or if the whole thing needs to be redone only using try, catch.
try
{
int value = int.Parse(Console.ReadLine());
if (value > 0 || value < 20)
{
Console.WriteLine(string.Format("You entered {0}",value));
}
}
catch(System.Exception)
{
Console.WriteLine("Value is out of bounds!");
}
1 Answer
JEFF CHAI
7,564 PointsBased on the query you provide, we can see that "if (value > 0 || value < 20)" - Meaning value is greater than 0 or value is smaller than 20, so when the user key in 25, it will prompt "You entered 25" because 25 is greater than 0. In order to use if statement to pass the challenges, you forget to add a else statement to print the error exception.
Let check back with the challenges it request you to prompt "Value is out of bounds!" when the user key in 25, you can do it like below code
try { int value = int.Parse(Console.ReadLine()); if (value >=0 && value <= 20) { Console.WriteLine(string.Format("You entered {0}",value)); } else { throw new System.Exception(); } }
catch(System.Exception) { Console.WriteLine("Value is out of bounds!"); }