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

Chris Ayers
Chris Ayers
554 Points

Solution for this problem?

The wording on this problem confuses me, a lot. Can someone explain what they did so I can better understand this.

Thanks

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);
    }
}
Chris Ayers
Chris Ayers
554 Points

The code in Main.java is going to throw an IllegalArgumentException when the drive method is called. Catch it and warn the user. This is what its asking for^

1 Answer

If you go back to the previous code assessment you can see the GoKart code. The problem with the current code is that when we create a go kart there are 0 bars of battery life. We then try to drive the kart without charging the battery. The drive takes two (2) bars of energy, which our new kart does not have yet, meaning we can't drive.

The try-catch stops the Illegal argument (trying to use 2 bars of battery when we have 0 bars of battery) from crashing the app. This is what the code challenge is trying to get you to think about, and why we have try catches in the first place, to save our program from crashing from our logical errors (or the user's).

public class Main {
    public static void main(String[] args) {
        GoKart kart = new GoKart("yellow");
        if (kart.isBatteryEmpty()) {
          System.out.println("The battery is empty");
        }



       /*
      Code that I entered
      */
      try {
        kart.drive(2); // This is where the problem is that we need to "catch"
      } catch (IllegalArgumentException e) {
        System.out.println("Illegal argument exception was thrown: " + e);
      }
    }
}
Chris Ayers
Chris Ayers
554 Points

Thank you! I had tried almost the exact same as your showing above so I must have had a } in the wrong place! Thank you for the better example as well! This really helps me out, I am used to using eclipse to do java code, so working on here has been different, but in a good way!

Thanks again!

I get this problem a lot, and it is hard to find out what I am doing wrong without the context of the other code. In time you will better recognize potential errors without the help of an IDE. Good luck!