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 to solve Drive and Catch Challenge?

The code in Main.java is going to throw an illegalArgumentException when the drive method is called. Catch it and warn user. Where is my code going 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);
    }
}
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(100);
    } catch (IllegalArgumentException iae) {
        System.out.println("Alert");
        System.out.printf("There was an error: %s\n", iae.getMessage());
      }

Hi Aaron,

I added markdown to help make the code more readable. If you're curious about how to add markdown like this on your own, checkout this thread on posting code to the forum . Also, there is a link at the bottom called Markdown Cheatsheet that gives a brief overview of how to add markdown to your posts.

Cheers!

1 Answer

I copy / pasted your code into the challenge and found that it needed two more closing curly braces at the end. Adding these passed the challenge.

Hope this helps,

Cheers

Thanks alot Robert! God Bless!