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

Derian McCrea
Derian McCrea
1,370 Points

Unsure of math equation used to check ranges.

I'm supposed to check if the value input is less than Zero or Greater than Twenty, then to throw an Exception(); .

Is my logic in how I approach the question correct?

Also what am I missing in my understand of this challenge.

Thanks in advance!

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

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

//unsure of math equation to check ranges below 0 and above 20
if (value = <0 && >20)

        {
            thorw System.Exception();
        }    
Derian McCrea
Derian McCrea
1,370 Points

So I figured it out I had a spelling error on top of it BUT i had to cheat To actually get one aspect.

Why do i have to throw a "new" System Exception?

I don't understand what can't it be throw "System.Exception();" Is it because in this code it hasn't been previously defined?

2 Answers

Steven Parker
Steven Parker
231,275 Points

You're close, here's a few hints:

  • a single "=" is an assignment operator, you won't need that in a comparison
  • both sides of a logical operator must be a complete comparison (ex: "x > 5 && x < 13")
  • to test if either of two conditions are true, you need the OR operator ("||")

I'll bet you can do it now without a code spoiler.

Derian McCrea
Derian McCrea
1,370 Points

Steve Thanks for your feedback!

Matti Mänty
Matti Mänty
5,885 Points

Your logic seems, well, logical, but the syntax for checking that needs a value to compare to after each and/or etc. Also the &&-marking you used returns true only of both of the arguments are true. ||-marking returns true if one (or both) of the conditions are true.

if (value <= 0 || value > 20) // this returns true if value is 0 or less than zero AND 

Also greater and less comparisons need to happen before you check if it's equal, so in the same order as you would say it: "less/greater than or equal". (As a bonus remark, there's a typo in the word throw!)

Derian McCrea
Derian McCrea
1,370 Points

Thank-you for your feedback Matti!