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

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Cant solve the challange :(

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

  if (kart.drive()){
  throw new IllegalArgumentException("The Karts battery ist too low for the drive");

}

}

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)
      if (kart.drive() == true){
      throw new IllegalArgumentException("The Karts battary ist too low for the drive");

    }
}

Is it possible that you need a catch block? I have not taken this module but I'm assuming that is part of the problem?

1 Answer

I am not familiar with the problem but a few things I notice right off the bat.

The try statement does not take any parameters so it would look like this.

try{
    logic goes here.
}

Also when you need to use a try block you need a catch statement to go with it. So your block of code would look something like this.

try{
    logic
}
catch (someException e){
    System.out.println(e.getMessage());
}

But the way you have the logic set up you are try to mash together two different exception handling techniques. The way I think that you are trying to make this work your code should look something like this.

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

      if (kart.drive() == true){
          throw new IllegalArgumentException("The Karts battary ist too low for the drive");
    }
}

I am not amazing at explaining things clearly this link should help with differentiating between the two. https://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html

I hope this helps.