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

Chris Tapia
Chris Tapia
1,684 Points

Help Java Exceptions

Im not sure what I did 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(200);
      }
      catch(IllegalArgumentException iad) {
        System.out.printf("Error is %s", iad getMessage());

      }


    }
}

Where is the exception at? What is the exact error?

1 Answer

Hi Chris,

An error will be thrown when kart.drive(2) is called - we have to catch that error. Also, I noticed that your getMessage() call is not attached to the IllegalArgumentException object. To call a method on an object, use the dot . separator: iae.getMessage().

Edit: changed name of IllegalArgumentException object from iad to iae.

try {
  kart.drive(2);
} catch (IllegalArgumentException iae) {
  System.out.printf("%s", iae.getMessage());
}

Hope this helps,

Cheers