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#

TreehouseDefense Exception.cs compile error

I compiled the program and I got this error: Exceptions.cs(20,12): error CS1520: Class, struct, or interface method must have a return type. I'm not sure why. The exception class above it is exactly the same but dosen't produce an error.

namespace TreehouseDefense
{
  class TreehouseDefenseException : System.Exception
  {
    public TreehouseDefenseException()
    {
    }    
    public TreehouseDefenseException(string message) : base(message)
    {      
    }
  }  
  class outOfBoundsException : TreehouseDefenseException
  {
    public outOfBoundsException()
    {
    }    
    public OutOfBoundsException(string message) : base(message) //Error is here
    {      
    }    
  }
}

1 Answer

andren
andren
28,558 Points

The problem is that you have called the class "outOfBoundsException" but the constructor "OutOfBoundsException". Constructors have to be named exactly the same as the class they are in, if they are not then they are treated like a regular method, which needs to have a return type specified.

If you change the name to be consisted with the class name then the error should go away. Though its also worth noting that class names are usually written with all words starting uppercase (Pascal Case) not in Camel Case like methods usually are.

So the class should actually be named "OutOfBoundsException" rather than "outOfBoundsException" like it currently is.

Thank you. I keep overlooking the little details.