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

Pauliina K
Pauliina K
1,973 Points

IllegalArgumentException

I get an error here that I don't understand. I can't see the error in the preview, a note just comes up saying: "Bummer! Make sure you throw the exception before change the mBarsCount field." So I'm guessing I put my exception i the wrong place, but then where is the right?

--

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;
     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;
  }

  }

PS. I don't understand how you make it look like code here in the comments? Just some parts looks right or it looks like this.

Pauliina K
Pauliina K
1,973 Points

Ugh.. I tried to put the exception under the other drive-method, but I then got this error message instead:

"Bummer! Are you sure you threw the IllegalArgumentException if the requested amount of laps would make the battery less than zero?"

I don't get it. Where is it supposed to be then? Isn't this all the code you need to throw an exception? throw new IllegalArgumentException("Not enough battery remains")

Or do I need to add something? I tried adding a "if(laps > mBarsCount)", but that didn't work either.

-- Steve Hunter, do you know what I'm doing wrong? (I don't know anyone else here, and you've helped me before)

1 Answer

Hi Pauliina,

You need to see if laps is greater than mBarsCount. If it is, throw the exception; else do the deduction:

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

Make sense?

Steve.

Pauliina K
Pauliina K
1,973 Points

Ahhh! I was so close!! I had everything except the else mBarsCount -= laps;

Btw, how do you make the code look like code here in the comments? I can't seem to get it right, hehe.

You were very close, yes!

To get the code to look like code, use three backticks (top left on a Windows keyboard, underneath escape) then the language name.

Close the code block with another three backticks on their own.

I edited your initial post so if you go and hit the three dots, and select Edit, you'll see what I added to make it work.

Steve.

Pauliina K
Pauliina K
1,973 Points

Awesome! Thanks again Steve!

No problem. :-)