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 trialTojo Alex
Courses Plus Student 13,331 PointsI'm not exactly sure what to do in this code challenge
For this code challenge I don't know what I 'm supposed to do. Please don't show me the code but just explain.Thanks.
int value = int.Parse(Console.ReadLine());
if (value < 0 || value > 20)
{
throw new System.Exception();
}
Console.WriteLine(string.Format("You entered {0}",value));
8 Answers
Steven Parker
231,269 PointsWhen the instructions say "wrap the testing logic with a try/catch...", they mean that all the code here (or at least everything after the ReadLine) should go inside a "try" code block. After that, a "catch" block should be added to contain the output statement for the new error message.
Tojo Alex
Courses Plus Student 13,331 PointsI have stored the code in the Untitled file
Steven Parker
231,269 PointsI see you fixed the spelling, but you still need to make the other two changes.
Tojo Alex
Courses Plus Student 13,331 Pointsare u sure the messages are reversed
Steven Parker
231,269 PointsRemember that the code in the "catch" will only run when the exception is thrown because the value is out of range. And the other message will only be shown after you move it outside the conditional block.
Good rule of thumb: There should never be any code in the same block after a "throw", a "return" or a "continue".
Tojo Alex
Courses Plus Student 13,331 Pointsoh thanks for letting me know https://w.trhou.se/nha0jdndav
Steven Parker
231,269 PointsI'm not sure why you posted another snapshot before fixing the issues. The success and error messages are still reversed, and the line to output the success message must be moved outside of the conditional ("if").
Tojo Alex
Courses Plus Student 13,331 Pointsoh sorry thats a mistake
Tojo Alex
Courses Plus Student 13,331 Pointsnow I all I have to do is " the line to output the success message should remain outside of the conditional " though I don't fully understand what you mean about that.
Steven Parker
231,269 PointsFollowing the "if" statement is a code block in braces that is only executed if the condition is true. This conditional code block contains the "throw" statement. Your snapshot showed the output message in that same block, but it needs to be moved outside (after) that block so it will be executed when the condition is not true.
Tojo Alex
Courses Plus Student 13,331 Pointsshould it be in else statement
Steven Parker
231,269 PointsAnytime an "if" block ends with "throw", "return", or "continue", no "else" is needed because the code flow will be diverted
Tojo Alex
Courses Plus Student 13,331 Pointsthanks I completed the challenge
Tojo Alex
Courses Plus Student 13,331 PointsTojo Alex
Courses Plus Student 13,331 PointsOk thanks I think I've done that and I now have this error: StudentsCode.cs(7,10): error CS1525: Unexpected symbol
System', expecting
(' Compilation failed: 1 error(s), 0 warnings This is my code: using System;int value = int.Parse(Console.ReadLine()); try{ if (value < 0 || value > 20) { throw new System.Exception(); Console.Writeline("Value is out of bounds!"); } } catch{ Console.WriteLine(string.Format("You entered {0}",value)); }
Steven Parker
231,269 PointsSteven Parker
231,269 PointsIt's hard to read compacted code! Use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. Or watch this video on code formatting.
But I spotted a few issues: