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

Alvaro Gutierrez
Alvaro Gutierrez
1,471 Points

I am stuck on: The code in Main.java is going to throw an IllegalArgumentException when the drive method is called...

Not sure what else to try.

      try {
        kart.drive(2);
      }catch (IllegalArgumentExeption iae) {
        System.out.println("Oh oh!!");
        System.out.printf("Your battery is dead", iae.getMessage());

        ```

2 Answers

i can see you left out curly braces. Try this code bellow. if it works click best answer and if it doesnt give me a shout.

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);
      }catch(IllegalArgumentException iae)
      {
      System.out.println(iae.getMessage());
      }
    }
}
Alvaro Gutierrez
Alvaro Gutierrez
1,471 Points

That worked! Thanks! I had all those brackets, just didn't include them into the forum. The only difference was the bracket after catch. On the videos he has the bracket after catch like this:

}catch (IllegalArgumentExeption iae) {

you had the bracket on the bottom like this:

 }catch(IllegalArgumentException iae)
      {

What is the difference?

please not that u left out the '%s' which is supposed to be replaced by the massage in iae in this statement.

System.out.printf("Your battery is dead", iae.getMessage());

the correct version is as below. please click best answer if it helps and give me a shout if it doesnt

System.out.printf("Error: %s", iae.getMessage());

or just this

System.out.printf(iae.getMessage());
Alvaro Gutierrez
Alvaro Gutierrez
1,471 Points

Thank you for your quick response. It still isn't working. The error I am getting is: ./Main.java:9: error: cannot find symbol }catch (IllegalArgumentExeption iae) {

Here is my 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(2);
      }catch (IllegalArgumentExeption iae) {
        System.out.println(iae.getMessage());
         }

      ```