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

Scott Tucker
Scott Tucker
3,765 Points

C# Objects Catching Exceptions challenge task help.

I had no trouble at all with the Throwing Exceptions challenge but this one is stumping me. I know that the code that goes into the try block is the code that throws the exception and I thought that my code would work but it doesn't. Does anyone have any thoughts on how to fix this? Any answers would be appreciated.

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

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

Console.WriteLine("Value is out of bounds!");
}

3 Answers

Steven Parker
Steven Parker
231,268 Points

Well, you said it right: "the code that goes into the try block is the code that throws the exception". But you didn't actually put the code with the throw inside the try block! Plus, you duplicated the input (ReadLine) and lost the original (normal) output.

Doing what you described would look more like this:

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

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

Ah, thank you! I had a feeling I was over complicating it. I didn't know that the "throw new System.Exception();" line could go into the try statement.

And thank you for responding quick.

Steven Parker
Steven Parker
231,268 Points

The timing was pure luck. But happy to help!

Henrique Amaral Silva
Henrique Amaral Silva
4,429 Points

Does it work? Mine is similar and I got: " Bummer! Did you throw an exception when the value is less than 0 or greater than 20?"

Scott Tucker
Scott Tucker
3,765 Points

Henrique Amaral Silva Yes, the answer does work. You need the if statement inside the try statement. You could also be using >= instead of >.

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

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

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

Hi, not sure what i'm doing wrong here.

I've tried a few different versions of the above including resorting to copying and pasting the suggested code from above but i still get: "Bummer! Did you throw an exception when the value is less than 0 or greater than 20?"

In the if statement i've tried (value < 0 || value > 20 ), (value <= -1 || value >= 21) and i've even split the two conditions into an if else statement but I still get the same error, am i going crazy!?

same here