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

Michael LaCroix
Michael LaCroix
5,828 Points

Confused

I followed Craig's example exactly it's still telling me that I didn't catch the exception. If that's the case then I don't understand what part of his code is 'catching' the exception. I have every part that he had.

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(20);
  }  catch (IllegalArgumentException iae){
  System.out.println("Too Many Laps");
   System.out.printf("Problem: %s\n", iae.getMessage());
      }
    }

 }

3 Answers

The challenge is asking you to put the current drive method call into a try statement and then catch the IllegalArgumentException. In your code, you call the drive method twice.

Simply replace the kart.drive(20); with kart.drive(2);

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Michael;

Welcome to Treehouse!

You are right on the money with your try... catch block. The thing that is holding you up in this challenge is the original calling of kart.drive(2); from the prompting code.

If you remove that line it passes the challenge with the thought being that you want to make sure that whenever you are "driving" you are doing so inside the try... catch. Make sense?

Ken

Michael LaCroix
Michael LaCroix
5,828 Points

Thanks, Ken. I've apparently made a mistake in assuming all the pre-written code is correct.

I'm still pretty confused: 1) The mistake in my code was not "driving" inside the try...catch block. Was I really supposed to learn that from Craig's 2 second reference to putting what "you think will be unsafe" in Try? 2) The error in my code was in the try portion so why was it telling me that I wasn't catching it? 3) It's only 2 laps. The code we wrote prior should only catch it if it's more than the battery can handle, so I'm confused as to why 2 laps was caught. It needs to be at least 9, no?

Ken Alger
Ken Alger
Treehouse Teacher

Michael;

1) I have forgotten the quantity of time that was spent on referencing "unsafe" code options. That being said, that is kind of the point behind try... catch, right? We want to try something that may pose an issue, in this example to make the cart drive too far. In other applications we could try to access a database and catch any errors if that database is unavailable, for example.

2 & 3) The code challenge checking engine attempts to, as near as I have been able to figure out, check for errors based on what the concept the challenge is asking. Different instructors at Treehouse utilize the checking engine in different ways and I cannot speak directly to what it was looking for in this instance, but I'll hazard a guess. I would think that the checking engine looks to make sure that all instances of kart.drive() are inside a try... catch block. Since you had one outside the block, the challenge engine threw an error. For your point on item 3, I think it would fall into my previous statement as well.

The thought there being that for the challenge itself the battery may be full, but if we are driving two laps without checking the battery level, eventually we will run out of battery, right? That fifth time of calling kart.drive(2) will bugger us up if it is not inside a try... catch.

Does that help at all?

Ken