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

krishna allu
krishna allu
745 Points

I cant able to find the error.

Help me to find error?

Program.cs
int value = int.Parse(Console.ReadLine());   
if(value < 0  || value >20)
{
Console.WriteLine(string.Format("You entered {0}",value));
}
else
{
throw new System.Exception("Outside of the Range");
}

1 Answer

andren
andren
28,558 Points

Your code is right but the logic is off, or reversed more accurately. The challenge wants the exception to be thrown if the value is less than 0 or greater than 20. Your code throws the exception if the value is not less than 0 or greater than 20.

If you swap the code in your else block with the code in your if block like this:

int value = int.Parse(Console.ReadLine());   
if(value < 0  || value >20)
{
    throw new System.Exception("Outside of the Range");
}
else
{
    Console.WriteLine(string.Format("You entered {0}",value));
}

Then your code will pass.