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 Loops and Final Touches While Loops

oreth
oreth
1,938 Points

On path exception - is my solution acceptable?

Hi, I am new to programming, and as I saw few people also had problems with creating the OnPathException. When I was checking what other people did, most of them used loops which I don't know yet. Therefore I tried doing this my own way. My code works, however, I feel like it's not the best solution. So first I created OnPath method in the Path class:

 public bool OnPath(Point point)
      {
        return point.Y == _path[0].Y;
      }

I didn't check every position in the _path array since if Y of the tower is the same as the Y of any map location in the path it's always on the path. (I am aware that it only works if the path is a straight line but I wanted to keep it simple. )

Then in Tower constructor, I wrote:

public Tower(MapLocation location, Path path)
    {
      if(path.OnPath(location))
      {
        throw new OnPathException("This tower is on the path!");
      }
      else
      {
        _location = location;
      }

and I created new exception:

class OnPathException : TreehouseDefenseException
  {
    public OnPathException()
    {
    }

    public OnPathException(string message) : base(message)
    {
    }
  }

And to my surprise, it works. However, I would like to hear from more experienced people if this code is acceptable. Should I change it or leave it? Any tips?

Cheers!