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 trial

C# C# Objects Inheritance Catching Exceptions

Tojo Alex
PLUS
Tojo Alex
Courses Plus Student 13,331 Points

I'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.

Program.cs
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
Steven Parker
231,275 Points

When 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
Tojo Alex
Courses Plus Student 13,331 Points

Ok 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
Steven Parker
231,275 Points

It's hard to read compacted code! Use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.

But I spotted a few issues:

  • the line to output the success message should remain outside of the conditional
  • "Writeline" (with lower-case "l") should be "WriteLine" (with capital "L")
  • the success and error messages seem to be reversed (in the opposite locations)
Steven Parker
Steven Parker
231,275 Points

I see you fixed the spelling, but you still need to make the other two changes.

Steven Parker
Steven Parker
231,275 Points

Remember 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".

Steven Parker
Steven Parker
231,275 Points

I'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
PLUS
Tojo Alex
Courses Plus Student 13,331 Points

now 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
Steven Parker
231,275 Points

Following 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.

Steven Parker
Steven Parker
231,275 Points

Anytime an "if" block ends with "throw", "return", or "continue", no "else" is needed because the code flow will be diverted