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 trialPauliina K
1,973 PointsIllegalArgumentException
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.
1 Answer
Steve Hunter
57,712 PointsHi 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
1,973 PointsAhhh! 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.
Steve Hunter
57,712 PointsYou 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
1,973 PointsAwesome! Thanks again Steve!
Steve Hunter
57,712 PointsNo problem. :-)
Pauliina K
1,973 PointsPauliina K
1,973 PointsUgh.. 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)