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#

sharon smith
sharon smith
10,527 Points

C# try/catch quiz // Challenge Task 1 of 1

I think the attached (can't attach? / posted below) should work, but am getting the message: Bummer! Did you add a catch block to handle the thrown exception?

Here’s the link: https://teamtreehouse.com/library/c-objects/inheritance/catching-exceptions-2

If incorrect, would like to understand why.

***** code *****

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

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

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

6 Answers

Steven Parker
Steven Parker
229,785 Points

The "throw" must be inside the try block.

So you just need to move the beginning of the try block up to include the if with the throw inside.

Hi Steven, thanks for this guidance. Is this applicable only to this particular code challenge, or is this a rule in general applicable to ALL situations, that the "throw" must be inside the try block?

James Thomas
James Thomas
6,452 Points

Whilst that works this is a rather poorly thought out coding challenge in that case, as it is not clear to the student at this point of the c# tuition that the try block should include all of that; This is due to the previous video teaching the user to put a throw new exception handler inside of a class and to try and catch it in another class.

To anyone reading this with more coding knowledge than students at this level I may sound like an idiot but trust me, this question makes it quite unclear what's going on. Just some hopefully useful feedback from me.

Steven Parker
Steven Parker
229,785 Points

The forum is a great place to get help from other students, but may not be the most expedient (or even certain) way to contact the staff.

You can make suggestions about a course directly to the staff as described on the Support page.

James Thomas
James Thomas
6,452 Points

Mind >>>>>>>>>>>>>>>>>>> Blown!

sharon smith
sharon smith
10,527 Points

yep / that works:) many thanks!

James Thomas
James Thomas
6,452 Points

Okay so now, I am actually getting the error message: "Bummer! I entered "25". I expected "Value is out of bounds!" but got "Value is out of bounds" instead."

What on earth am I supposed to do with that information??

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(System.Exception) { Console.WriteLine("Value is out of bounds"); }

Steven Parker
Steven Parker
229,785 Points

The challenges can be rather picky about output strings. If you look closely at the "Bummer" message:

  • ... I expected "Value is out of bounds!" :point_left: notice the exclamation point
  • ... but got "Value is out of bounds" :point_left: exclamation point is missing

For future questions, please ask a new question instead of adding a question as an answer to a different question. This will give you a better chance of a rapid response.