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

KHARAK SINGH
KHARAK SINGH
1,061 Points

Error message "Looks like you didn't catch the exception".

I am getting this error in my program.

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(10);
        System.out.println("fuuuuuuuuuussssssssssssssssss.");
      } catch (IllegalArgumentException iae) {
        System.out.println("Whoa there!!!");
        System.out.printf("The problem was: %s", iae.getMessage());
      }
    }
}

1 Answer

Sjors Theuns
Sjors Theuns
6,091 Points

Hi,

the IllegalArgumentException is thrown by calling the drive method. The first time you call this method it is outside the try catch block.

Set the try { a bit higher like so:

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);
        kart.drive(10);
        System.out.println("fuuuuuuuuuussssssssssssssssss.");
      } catch (IllegalArgumentException iae) {
        System.out.println("Whoa there!!!");
        System.out.printf("The problem was: %s", iae.getMessage());
      }
    }
}