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#

Gil Huss
Gil Huss
2,750 Points

C# question I seam to answer right but gets me a "Bummer" error.

Challenge Link: https://teamtreehouse.com/library/c-objects/inheritance/catching-exceptions-2

Question: "Here's how I wrote the code. Now wrap the testing logic with a try/catch and write the message "Value is out of bounds!" to the console if the exception is caught."

Code from the Challenge:

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

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

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

My Answer:

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

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

here the "Bummer" msg:

"Bummer! I entered "25". I expected "Value is out of bounds!" but got "Value is out of bounds" instead."

which must be right, if he got the answer he expected right?

Here's the right answer I found with the help of the community:

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

I used an "if -- else" method and they just copied the text after the if, which in this case should have the same outcome? Doesn't it? This is just a question for why the way I think is "wrong" or if it should be right after all? Thanks for your time and help! Gil

1 Answer

Steven Parker
Steven Parker
231,007 Points

:point_right: It looks like you forgot the punctuation.

Since the throw diverts the program flow, the "else" is not necessary; but it doesn't hurt. But the challenges are very picky about result strings, and you omitted the punctuation when you wrote "Value is out of bounds".

It should be "Value is out of bounds!" :point_left: Note the exclamation point.

Gil Huss
Gil Huss
2,750 Points

oww I get it now! thanks for your feedback Steven! :)