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

Chelsea Casareale
Chelsea Casareale
18,545 Points

Code not working? (code challenge task)

So in the previous task I made the 'throw new IllegalArgumentException' for the GoKart class so it should trigger when the integer passed in drive is greater than 8.

Now I'm on the try and catch portion of the challenge and it's saying I didn't catch the exception as my error? It's not giving me a syntax error. What did I goof up?

Thanks!

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.printf("Error: %s\n", iae.getMessage());
      }
    }
}

2 Answers

John Armini
John Armini
11,981 Points

The challenge was saying calling the drive method will throw an Illegal Argument Exception, but I don't see the part specifying it will throw that exception if the integer is greater than 8. So it looks like you wrote the code correctly you just have to wrap the

kart.drive(2)

line in your try/catch block.

Chelsea Casareale
Chelsea Casareale
18,545 Points

Oh geez I didn't even think of that! haha

Thanks - that did the trick!