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

LESTER POLICARPIO
LESTER POLICARPIO
2,139 Points

Did you throw an exception when the value is less than 0 or greater than 20?

Hi I am getting the error "Did you throw an exception when the value is less than 0 or greater than 20?" although I already did that using "throw new System.Exception()";

What am i doing wrong?

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

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

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

}

catch(Exception)
{
    Console.WriteLine("Value is Out of Bounds!");
}

2 Answers

Jon Wood
Jon Wood
9,884 Points

Your code is great! You pretty much have it, but for the sake of the challenge, I think you may be doing too much. :P

I don't think you need the try/catch block for this challenge. It seems to be expecting an exception, but here you're catching it and writing to the console. Try that and see how it goes.

Again, you're code is just more than what the challenge expects, so you're doing great!

LESTER POLICARPIO
LESTER POLICARPIO
2,139 Points

Ohhh you're right =) thanks I might got over excited and put all what I've learned from the previous video

Cheers,

Jon Wood
Jon Wood
9,884 Points

Nothing wrong with that! :)