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

Java Java Objects (Retired) Harnessing the Power of Objects Throwing Exceptions

Ben Mair
Ben Mair
8,728 Points

Exception Throwing

So I wrote in the exception however it wants me to throw the exception prior to the mBarsCount? I'm confused..........

GoKart.java
public class GoKart {
  public static final int MAX_BARS = 8;
  private String mColor;
  private int mBarsCount;

  public GoKart(String color) {
    mColor = color;
    mBarsCount = 0;
  }

  public String getColor() {
    return mColor;
  }

  public void drive() {
    drive(1);
  }

  public void drive(int laps) {
    // Other driving code omitted for clarity purposes
    mBarsCount -= laps;
    if (mBarsCount == 0); {
      throw new IllegalArgumentException("Not enough battery remains");
    }
  }

  public void charge() {
    while (!isFullyCharged()) {
      mBarsCount++;
    }
  }

  public boolean isBatteryEmpty() {
    return mBarsCount == 0;
  }

  public boolean isFullyCharged() {
    return mBarsCount == MAX_BARS;
  }

}

5 Answers

Ben Mair
Ben Mair
8,728 Points

I have rewatched the video and am doing some research online.........why would it be if(newBarsCount < MAX_BARS? wouldn't that just ask it to throw the illegal argument exception if newBarsCount is less than 8? Why would it not be == 0?

From looking at other forum answers, am I right in thinking that there is actually more than one answer that would work? I used..

if (laps > mBarsCount) {
  throw new IllegalArgumentException("Not enough battery remains");
}
mBarsCount -= laps;
}

.. and it worked :)

I've seen a forum answer too where they use the 'else' line - is that actually necessary? Won't it do whatever is on the next line if the exception isn't thrown?

One of my biggest issues is knowing where to put the code. Could it have gone within the other method?

public void drive() {
  drive(1);
  if (laps > mBarsCount) {
     throw new IllegalArgumentException("Not enough battery remains");
  }
  mBarsCount -= laps;
}

Actually, thinking about it.. would this give me a compile error as it is referencing variables that haven't been defined? I think I'm confusing myself more! Can someone help un-muddy the water please?

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Since you changed mBarsCount, even though the exception is thrown, the state of the object changed. That is what we are trying to prevent by throwing the exception. What if you stored the result in a new value, and checked against that? If you get past that check, only then change the private member variable mBarsCount.

Hope that helps!

Ben Mair
Ben Mair
8,728 Points

K......So I changed it and am still having issues

I did the public drive Etc Line int new totalBars = mBarsCount -= laps; if (totalBars == 0); { throw new IllegalArgumentException("Not enough battery to finish") }

Romel Liwag
Romel Liwag
4,183 Points

I think you need to rewatch the whole Exception Throwing video. but ill give you the answer.

public void drive(int laps) {
    int newBarsCount = mBarsCount - laps;
    if(newBarsCount < MAX_BARS){
      throw new IllegalArgumentException("Not enought battery remains");
    }
  }

Have fun coding :)