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

Jeongeui Ji
Jeongeui Ji
3,761 Points

I do not understand... Please help me.

I think I coded the right condition of the if statement as not to be less than 0 or greater than 20. And then I threw an exception afterwards. Did i do something wrong? Thanks in advance.

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

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

if(!value < 0 || value >20)
{
    throw new System.Exception();
}
Kevin Agpaoa
Kevin Agpaoa
10,503 Points

!value < 0 is the same as value > 0. To correct the problem you can do value < 0 or !(value >0), so values 1-19 will be excluded from the exception.

1 Answer

andrewgabriel
andrewgabriel
18,106 Points

I would just add () around value < 0 and leave ! outside so that way it evaluates the whole statement. Before that you were modifying value to false since you didn't include < 0 within that !(). So it should look like this:

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

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

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

Interesting question!