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

almost there but a little confused

little help here

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(4);
        System.out.println("This will never happen");
      }
      catch (IllegalArgumentException iae) { 
       System.out.println("Check battery level!");
        System.out.printf("The error was: %s\n, iae.getMessage");
      }
}

1 Answer

Alex Johnson
Alex Johnson
6,067 Points

There are a few issues here, but the heart of the matter is that the method kart.drive() is being called twice, once before the try/catch block and once from within the try block. In order for the exception to be caught it needs to be called from within the try block only.

In addition, there are some curly braces out of place and the printf method is formatted incorrectly. It should look like this:

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

Make those changes (correct placement of kart.drive(), fixed curly brackets, correct formatting of printf method) and let me know if it works.