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

Judith Houser
Judith Houser
3,405 Points

Getting error on here:

I really would like help seeing what I am missing as I dont even have an ">" on that line.

2 Answers

Hi, you didn't post your solution so I'm not sure what may have happened in regards to the greater than(>).

However, the challenge is asking you to wrap the code it gives you in a try catch block. You don't need to modify the few initial statements it provides, just add code around them:

try 
{
    int value = int.Parse(Console.ReadLine());
    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!"); 
}

If you are confused what the try block is trying to do, it's as follows:

  • assign the variable 'value' the users input (converted to an int value- as Console.ReadLine returns a string as default).
  • then if the value of the variable 'value' is less than 0 OR greater than 20 throw an exception,
  • else the console will print the users value such as "You entered 5".
  • in the event an exception is thrown, the catch block is executed and "Value is out of bounds" is printed to the console instead.
Judith Houser
Judith Houser
3,405 Points

I got it! Thank you so much!!