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

Rachel Xie
Rachel Xie
5,824 Points

really don't understand the question requirement

I'm sorry. But I really don't quite understand the requirement here?

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

    try {
        if(value>=0 && value<=20)
        {Console.WriteLine(string.Format("You entered {0}",value));}
        }

    catch
        {Console.WriteLine("The value is out of bounds!");}

1 Answer

andren
andren
28,558 Points

The challenge asks you to throw an exception, it does not ask you to catch an exception. throwing exceptions is done by using the "throw new" keywords followed by the exception you want to throw an a pair of parenthesis like this:

throw new System.Exception();

In your code you check if the value is valid, but you don't throw any exception if it isn't. If you remove the try..catch block and just add an else clause to your if statement where you throw an exception as shown above, then you will be able to pass the challenge.

Rachel Xie
Rachel Xie
5,824 Points

Thank you very much, Andren~! Your explanation is so clear. Now I fully understand what I should do~!