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 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
231,248 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));