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

Devin Scheu
Devin Scheu
66,191 Points

Another Java Code Question

Question: The code in Main.java is going to throw an IllegalArgumentException when the drive method is called. Catch it and warn the user

Code:

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());
        }
        kart.drive(2);
    }
}
Stone Preston
Stone Preston
42,016 Points

I recategorized your question as Java, not Javascript

2 Answers

Stone Preston
Stone Preston
42,016 Points

the original code is:

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);
    }
}

the drive method should only be called once. remove kart.drive(2) from the end of your code.

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());
        }

    }
}
Devin Scheu
Devin Scheu
66,191 Points

Thanks again Stone, sorry for the trouble.

Stone Preston
Stone Preston
42,016 Points

no worries. was not any trouble at all

hi whats iae mean in this case and whats println ?

Jeff Collins
Jeff Collins
24,709 Points

Craig explains what iae is at around 3:55 in the exceptions video. It's an abbreviation for Illegal Argument Exception.