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

Christian Fincher
Christian Fincher
3,065 Points

How is my code not reaching the Catch statement?

Extremely confused as to why my code here is not reaching the Catch statement. Any assistance would be helpful

Program.cs
string value = Console.ReadLine();
int Value = 0;

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

try
{
   Value = int.Parse(value);
}
catch(Exception)
{
    Console.WriteLine("Value is out of bounds!");
}

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

1 Answer

Ben Reynolds
Ben Reynolds
35,170 Points

First I'd refresh the challenge and get back to the starter code, looks like you've added/modified some variables (the initial "value" int should be the only needed to complete this one.)

The key thing in the instructions is "wrap the testing logic with a try/catch", make sure the if statement is happening inside the try block.