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

Not sure when to use try/catch or if/else

On a very general level, I under try/catch, but I have a hard time separating it from the concept of if/else. This is where I'm at, but getting the error "Did you throw an exception when the value is less than 0 or greater than 20?"

Program.cs
int value = int.Parse(Console.ReadLine());
if (value > 0 || value < 20)
{
    try
    {
    Console.WriteLine(string.Format("You entered {0}",value));
    }
    catch(System.Exception)
    {
    Console.WriteLine("Sorry, that value is outside the acceptable range.");
    }
}

1 Answer

Steven Parker
Steven Parker
231,269 Points

In this challenge you only need "if". I see you have one already (but you may need to reverse the comparisons to fit the requirements), and inside the conditional block the challenge wants you to throw an exception.

And that's all there is to this one. You won't need a try/catch or "else" or any additional output statements.