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

Lee Morgan
Lee Morgan
2,419 Points

Not catching the exception

When I try submit this, it keeps telling me that it is not catching the exception. What have I done wrong here so that it is not catching it?

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);
      try {
        kart.drive(100);
        System.out.println("This will never happen.");
      } catch (IllegalArgumentException iae) {
         System.out.println("Not enough battery.");
      }
  }
}

2 Answers

Brandon Zeck
Brandon Zeck
3,127 Points

Hi Lee Morgan,<p>

Here, you were not asked to add a new drive() method.</p><p>

Instead, you were asked to catch the exception that may be thrown when the existing drive() method is called.</p><p>

In order to pass the challenge, you may Either use a try-catch block with the existing drive() method OR will have to ensure that every call to the drive() method is enclosed by a try-catch block.</p><p>

So, either of the code below should work.</p>

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());
      }
    }
}
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());
      }
      try {
        kart.drive(100);
        System.out.println("This will never happen.");
      } catch (IllegalArgumentException iae) {
         System.out.println(iae.getMessage());
      }
    }
}

<p>

Hope this helps.</p><p> Please Up vote if it helps. </p><p> If you feel this is the answer that you were looking for, please Mark as 'Best Answer'</p><p> And in case, it does not, please reply so that we will get to know.</p><p>

Thanks.</p>

try {
        dispenser.load(400); 
      } catch (IllegalArgumentException iae) {
        System.out.println("Whoa there!");
        System.out.printf("The error was: %s%n", iae.getMessage());
      }

Above, is the code example to follow. Instead of dispenser.load(400), you are trying kart.drive(2)