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

Rachelle Wood
Rachelle Wood
15,362 Points

Code worked in workspaces but not in challenge

I have checked, rechecked and rechecked, but I can't get this code to work in the challenge. It works great in workspaces though. What am I doing wrong?

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(100);
        } catch (IllegalArgumentException ex) {
        System.out.printf("Error message: %s\n", ex.getMessage());
      }
    }
}
Rachelle Wood
Rachelle Wood
15,362 Points

I get an error message saying that IllegalArgumentException was not caught.

1 Answer

Get rid of the line right before your try statement. The one that says kart.drive(2) It's not catching the exception on that call to the method.

Rachelle Wood
Rachelle Wood
15,362 Points

Hi Corey. Thanks so much!! I thought that since kart.drive(2) was outside of the try catch block that it would not be in conflict with it. Live and learn as they say :)