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 Handling Exceptions

I do not understand what Im doing wrong

Ive been stuck on this forever, I think this is the right code, but I cant get past it.

Main.java
public class Main {
    public static void main(String[] args) {
        GoKart kart = new GoKart("yellow");
        if (kart.isBatteryEmpty()) {
          System.out.println("The battery is empty");
        }
        kart.drive(2);
      try
      {
        kart.drive(200);
      }catch(IllegalArgumentException iae)
      {
        System.out.println("The error was: %s", iae.getMessage());
      }
    }
}

2 Answers

Hi Anjita,

I see a couple issues with your code. The drive method is already being called - don't need to call it again. Wrap your try / catch block around kart.drive(2);

Also, when you catch the error, you're passing in a formatted string to println. Either just print iae using println() or change it to printf() and your formatted string will work.

Kind Regards

I did printf, its still not working :(

I did printf, its still not working :(

Can I get you to post the line of code that's using printf?

printf("The error was: %s", iae.getMessage());

Make sure System.out is preceding printf

System.out.printf("The error was: %s", iae.getMessage());

If this still doesn't work for you, please post all of the code you're trying, from the challenge.

We'll get through this, I promise!