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

Robert Fowlie
Robert Fowlie
4,807 Points

Not sure what the objective of this task is. Or why this isn't working.

If I take the writeline out of the catch bracket, the prompt tells me it wrote 25 and nothing got printed. 25 is out of the range so why does it need to get printed?

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

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

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

1 Answer

Steven Parker
Steven Parker
229,670 Points

You got too fancy. The challenge just wants you to perform the test and throw the exception if appropriate. You don't need the "try" and "catch" blocks.

Robert Fowlie
Robert Fowlie
4,807 Points

Thanks for the reply Steven. Took out the try/catch and it worked.