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

Steve Wofford
Steve Wofford
2,485 Points

Protect the call to kart.drive by handling the Illegal Argument Exception.

Ive looked up multiple examples for this and none work? please 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");
    }
    try {
    kart.drive(42)
  } catch(IllegalArgumentException)
      System.println("Not enough Battery");

}
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");
    }
    lapsDriven += laps;
    barCount -= laps;
  }

}

5 Answers

class Example {

  public static void main(String[] args) {
    GoKart kart = new GoKart("purple");
    if (kart.isBatteryEmpty()) {
      System.out.println("The battery is empty");
    }
    try{
kart.drive(42);
}catch( IllegalArgumentException e){
System.out.println(e);
}
  }

}

thats so odd, I double checked and the above code worked for me...

This worked for me as well.

let me give you a hint use a try catch block:

try{
//This method throws SomeException 
call();
}catch( SomeException e){
System.out.println(e);
}
Joshua Morillo
Joshua Morillo
2,562 Points

I was stuck in the same spot you were. I used First Last's hint to hunt down the answer.

Your code:

    try {
    kart.drive(42)
  } catch(IllegalArgumentException)
      System.println("Not enough Battery");

}  

Correct code:

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

}

I re watched Craig's lesson, homeandlearn, and turorialspoint to figure it out.

System.out.printf("Not enough Battery %s %n", 
                                 iae.getMessage());

actually you can just use: System.out.println(iae);, this will print out Not enough Battery, the message of iae.getMessage() is Not enough Battery, so you'd have it written twice with your code

Joshua Morillo
Joshua Morillo
2,562 Points

Thanks for pointing that out, First Last.

Steve Wofford
Steve Wofford
2,485 Points

Code still doesn't work. I'm not getting what is wrong.

Steve Wofford
Steve Wofford
2,485 Points

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

I tried the same exact code you gave me. Gives me a compilier error everytime.

try{
kart.drive(42);
}catch( IllegalArgumentException e){
System.out.println(e);
}

thats it

So, I'm a bit confused about your method...

Why does System.out.println(e) work in this case? Are we saying here that the computer should print out e, which is defined by IllegalArgumentException on the previous line, which was in turn defined in the other GoKart Program?