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

Helmuts Reinis
Helmuts Reinis
1,021 Points

This was't as hard as i thought it would be...

Was prodding around for last 20 minutes ... wondering is did something wrong here, but challenge was passed. Only thing that confuses me is the "throw new Exception();" ... can it be written in any other format under "catch" ?

Program.cs
int value = int.Parse(Console.ReadLine());
try
{
    if (value < 0 || value > 20)
    {
        throw new Exception();
    }

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

2 Answers

Steven Parker
Steven Parker
231,248 Points

:point_right: It looks like you passed, but did it the hard way.

Glad you thought it wasn't so hard! But you really only needed to add the test (if) and the throw inside it.

The try, catch, and the other throw are all not needed and don't actually do anything, since you end up throwing the same exception again anyway.

Helmuts Reinis
Helmuts Reinis
1,021 Points

Awesome... i seem to have overthinked the task. So this should be the simplest way to do it.

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

Or have a i misunderstood?

pet
pet
10,908 Points

Yes your have understood.