Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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!