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 Throwing Exceptions

Josh Boersma
PLUS
Josh Boersma
Courses Plus Student 2,486 Points

Refusing to compile, am I reading the question wrong?

Challenge is: In the Program.cs file, write code to check if the value integer is less than 0 or greater than 20. Throw System.Exception if the value is outside of the range.

I take this to mean throw the exception if the value within the range [0,20]. Multiple refusing to compile snowballed into me creating the class to have it check in a separate method. I'm not sure if that's how it's supposed to be done. I'm at work and thus can't read the compile errors. Any directional help would be fantastic.

Program.cs
using System;

namespace Treehouse {
    class Program {
        static void Main() {
            int value = int.Parse(Console.ReadLine());
            Console.WriteLine(string.Format("You entered {0}",value));

            try{
                check(value);
            }//try
            catch(Exception){
                Console.WriteLine("That number is invalid");
            }//catch
        }//main
            public check(int value){
                if(value >= 0 && value <=20){
                    throw new Exception();
                }//if
            }//check
    }//class
}// namespace

1 Answer

Steven Parker
Steven Parker
231,269 Points

You're working too hard! All you need for this challenge is a test ("if") and a throw. No need to create classes or methods.

But do notice that the instruction say to throw the exception "if the value is outside of the range.", so you'll need to revise the test expression.