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

Okay, so let's throw an IllegalArgumentException from the drive method if the requested number of laps cannot be comple

where am i going wrong.Am getting an error saying wrong start of expression.

 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() {

  public void drive(int laps) {
    // Other driving code omitted for clarity purposes

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

  public void charge() {
    while (!isFullyCharged()) {

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

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


3 Answers

Hi MUZ140564 Kudakwashe Jena, an IllegalArgumentException

  1. says a message, and

  2. stops the code.

So the problem here is that you subtracted the bars before you stopped the code. Think of it like this: So in your code, mBarsCount is at 0. If you subtracted from there before you stopped the code, then it would be at -1, then say "Not enough battery remains". But what you want is to stop the user from changing it to -1. To complete the challenge, put mBarsCount -= laps; after the IllegalArgumentException.

Jordan Brown
Jordan Brown
4,446 Points


There are <b>two problems</b> I am seeing with your code:

  • You do not want mBarsCount -= laps; coming before your if statement.
    • The reason being is that in this example you are checking to see if you have enough battery to run a lap at mBarsCount = 0.
    • By subtracting laps before the if statement you are allowing the mBarsCount -= laps; to execute.
    • Since that piece of code executed the if statement could not work properly.
  • Problem 2, you need to close your if statement with a curly brace }
    • The reason you need to do this is because the compiler will return Error: reached end of file while parsing
    • This program does not close because you're taking a curly brace away from it's job to make up for the missing one on the if statement.

I commented the problems on your original code below.

public void drive ( int laps ) 
    mBarsCount -= laps; // Code executes here. Not allowing the if statement to do its job.
    if ( mBarsCount < laps ) 
        throw new IllegalArgumentException ( "Not enough battery remains" );
}// This brace is used to close the if statement

Now by making the proper adjustments we have the correct code.

public void drive ( int laps ) 
    if ( mBarsCount < laps ) 
        throw new IllegalArgumentException ( "Not enough battery remains" );
    }                           // Curly brace } closes the if statement.
     mBarsCount -= laps;        // Code executes here. Allowing the if statement to run.

Good luck!


yals r amazzing thank you for the great help.