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

How far off am i?

Am I close to the answer or way off?

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");
        }
      try {
        kart.drive(2);
      }
      catch (IllegalArgumentException iae) {
        System.out.println("WORK DAM IT!");
        System.out.printf("Error was: %s\n", iae.getMessage());
      }
}
Mark Miller
Mark Miller
45,831 Points

You may be missing a curly brace at the end. I see the opening and closing braces for the 'if' and the 'try' and for the 'catch.' Then, it appears you have only one closing brace for both the class and for the 'main.' Add one closing curly brace at the bottom of the page. Then submit your code.

I passed it with the following code:

try { kart.drive(2); } catch (IllegalArgumentException.illObject) { System.out.println(illObject.getMessage()); }

1 Answer

missgeekbunny
missgeekbunny
37,033 Points

It looks like you are pretty close but you never throw the IllegalArgumentException. You need to do that before you can call it.

Mark Miller
Mark Miller
45,831 Points

If it catches an exception, then it has been thrown. You don't manually throw it when using the "try, catch" code block.