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 Harnessing the Power of Objects Handling Exceptions

how do i do this Protect the call to kart.drive by handling the IllegalArgumentException that is thrown when not enough

plz help

Example.java
class Example {

  public static void main(String[] args) {
    GoKart kart = new GoKart("purple");
    if (kart.isBatteryEmpty()) {
      System.out.println("The battery is empty");
    }
    kart.drive(42);
    public class GoKart {
  private String mColor = "red";

  // public constructor here
  public GoKart(String color) {
    mColor = color;
  }

  public  getColor() {
    return mColor;

  }

}
GoKart.java
class GoKart {
  public static final int MAX_BARS = 8;
  private int barCount;
  private String color;
  private int lapsDriven;

  public GoKart(String color) {
    this.color = color;
  }

  public String getColor() {
    return color;
  }

  public void charge() {
    barCount = MAX_BARS;
  }

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

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

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

  public void drive(int laps) {
    if (laps > barCount) {
      throw new IllegalArgumentException("Not enough battery remains");
    }
    class GoKart { public static final int MAX_BARS = 8; private String color; private int barCount; private int lapsDriven; // member variable private

public GoKart(String color) { this.color = color; }

public String getColor() { return color; }

public void charge() { barCount = MAX_BARS; } public boolean isBatteryEmpty() { return barCount == 0; }

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

}

    lapsDriven += laps;
    barCount -= laps;
  }

}

1 Answer

Joseph Wasden
Joseph Wasden
20,406 Points

So first, lets start with what we know. If we examine the drive method off the goKart class, we can see that the method is formed to throw an exception if the laps to be driven is greater than the bar count in the battery. So, the first half is done already.

whenever you work with a method that throws exceptions, it's a good practice to wrap them in a try...catch structure. This helps the exceptions to be handled gracefully. So, in this case, our method is invoked the following way:

   kart.drive(42); //<--note the method drive() here
    public class GoKart {
  private String mColor = "red";

let's wrap that in a try catch:

 try{
      kart.drive(42);
    } catch () {
      //code to execute on a caught exception goes here
    }

Now the code knows that some exception may occur during the execution of the drive() method. It still doesn't know what to do (or, in other words, how to handle the exception) when it is thrown. Did you notice how the catch portion takes a parameter? we need to declare the type of exception to catch there, and give it a name.

 try{
      kart.drive(42);
    } catch (IllegalArgumentException iae) {
      System.out.printf("%s", iae.getMessage());
    }

The exception is now named. I've also added the print code to save on time, since that is something you should be familiar with at this point.