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

Rajmohan Ganapathi
Rajmohan Ganapathi
1,405 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

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

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

Console.WriteLine(string.Format("You entered {0}",value));
try
{
if(value <0 || value >20)
{
   throw System.Exception;
}
}
catch(Exception)
{
   Console.WriteLine("value not in the permissible range");
}

4 Answers

Steven Parker
Steven Parker
231,198 Points

You're working too hard on this one! The instructions only ask you to perform the test and throw the exception. You won't need a "try", "catch" or error message code for this challenge.

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

{ if(value < 0 ||value > 20) { throw new System.Exception(); } } { Console.WriteLine(string.Format("You entered {0}",value)); } '''

is what finally worked for me. I got stuck on this for wayyyyy too long using try/ catch

Jude Brown
PLUS
Jude Brown
Courses Plus Student 629 Points

LOL - I did exactly the same thing. Back to the drawing board.

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

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

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