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

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

I can't seem to find out what its parsing at the end. I am able to complete most of it, but can't seem to get this. Can someone please help?

Main.java
public class Main {
    public static void main(String[] args) {
        GoKart kart = new GoKart("yellow");
        if (kart.isBatteryEmpty()) 
        {
        try {
          System.out.println("The battery is empty");
        } catch (IllegalArgumentException iae) {
          System.out.println("Whoa there!");
        }

2 Answers

Kourosh Raeen
Kourosh Raeen
23,733 Points

You need to put the line:

kart.drive(2);

in the try block. You also need to print out the exception message in the catch block:

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 e) {
          System.out.println(e);
        }
    }
}

I did that but it still keeps saying this: ./Main.java:11: error: reached end of file while parsing } ^ 1 error

Kourosh Raeen
Kourosh Raeen
23,733 Points

Can you post all your code in 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"); } try { kart.drive(2); } catch(IllegalArgumentException e) { System.out.println(e); }

Kourosh Raeen
Kourosh Raeen
23,733 Points

Make sure you have the correct number of closing braces, }, at towards the end of the code. There should be three of them, one for the catch block, one for the main method, and one for the class.

THANKS!! I finally solved it. It was the braces at the end.