Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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!!