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 Catching Exceptions

Entered "2", printed nothing... Catching Exceptions in C#

I cannot figure out where I'm going wrong here. I get no errors in the Preview. It looks like the variable VALUE is not meeting the criteria of the if statement.

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

try 
    {
        if (value < 0 || value > 20)
            {
                Console.WriteLine(string.Format("You entered {0}",value));
            }
    }
catch (System.Exception) 
    {
        Console.WriteLine(string.Format("Value is out of bounds!"));
    }

1 Answer

Dale Severude
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Dale Severude
Full Stack JavaScript Techdegree Graduate 71,350 Points

Two is printing nothing because you moved the Console.WriteLine inside of the if statement. So now it is printing a value only if the value is outside of the specified range. To get a value to print as expected, move the Console.WriteLine back outside of the if statement so it will print any number.

Then inside of the if statement, throw a System.Exception that will be caught when the number is out of range.