Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C# C# Objects Inheritance Throwing Exceptions

Jude Brown
PLUS
Jude Brown
Courses Plus Student 629 Points

In the Program.cs file, write code to check if the value integer is less than 0 or greater than 20. Throw System.Excepti

Sorry - I know that I am making this complicated but I don't know how else to do it. I'm looking at if/else, switches, try/catch.

Thanks in advance,

Jude

Program.cs
int value = int.Parse(Console.ReadLine());

try
{
    if (value < 0 || > 20)
}
cathc
{
throw(System.Exception);
}

else
  {
Console.WriteLine(string.Format("You entered {0}",value));
}

3 Answers

Steven Parker
Steven Parker
216,120 Points

You're working too hard! The instructions only say to check the value and thrown an exception. You won't need a "try" or "catch" or "else" or an error message to complete this task.

And as Edward pointed out, there's a missing variable in the conditional expression. The conditional should control the throwing of the exception.

Not sure how well I can help. I'm using my phone. The first issue is the value variable must be compared on both sides of the || value > 20.

Typo on catch. Try looking up how to use the catch statement. It needs some work, but I'm thinking your main problem is the comparison.

Jude Brown
Jude Brown
Courses Plus Student 629 Points

Right! Thanks - will give it a go tomorrow. Cheers.

Jude, Did you ever get this figured out?

Here is how I solved it using an if statement:

int value = int.Parse(Console.ReadLine());

if (value < 0 || value > 20)
{
    throw new System.Exception();
}

Console.WriteLine(string.Format("You entered {0}",value));