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 Harnessing the Power of Objects Handling Exceptions

Dennis Zern
Dennis Zern
813 Points

Can someone help me with these IllegegalArgumentExceptions? With a challenge from Java objects.

Challenge Task 1 of 1

Example.java contains a program using the GoKart class we have been building (included for you to reference).

Protect the call to kart.drive by handling the IllegalArgumentException that is thrown when not enough battery remains. Print out the message from the exception to the screen as you catch the exception. Important: In each task of this code challenge, the code you write should be added to the code from the previous task. Restart Preview Get Help Check Work Example.java GoKart.java

1 class Example { 2 ā€‹ 3 public static void main(String[] args) { 4 GoKart kart = new GoKart("purple"); 5 if (kart.isBatteryEmpty()) { 6 System.out.println("The battery is empty"); 7 } 8 kart.drive(42); 9 } 10

11

12 ā€‹ 13 } 14 ā€‹

WHAT am i supposed to write here? Explain why please if so.

Example.java
class Example {

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



}
GoKart.java
class GoKart {
  public static final int MAX_BARS = 8;
  private int barCount;
  private String color;
  private int lapsDriven;

  public GoKart(String color) {
    this.color = color;
  }

  public String getColor() {
    return color;
  }

  public void charge() {
    barCount = MAX_BARS;
  }

  public boolean isBatteryEmpty() {
    return barCount == 0;
  }

  public boolean isFullyCharged() {
    return MAX_BARS == barCount;
  }

  public void drive() {
    drive(1);
  }

  public void drive(int laps) {
    if (laps > barCount) {
      throw new IllegalArgumentException("Not enough battery remains");
    }
    lapsDriven += laps;
    barCount -= laps;
  }

}

7 Answers

Don't change GoKart.java. They want you to protect kart.drive(42) by wrapping it in a try-catch block.

Remember, they look like this:

    try {

    } catch(Exception e) {

    }

Example.java contains a program using the GoKart class we have been building (included for you to reference).

When you see the phrase 'included for you to reference', know that you will not change it, and that the answer will probably contain a method from the class they included for you to reference.

In this case, we did not need to reference the class since they told us what error to expect.

Dennis Zern
Dennis Zern
813 Points

Where am i supposed to write it?

You are supposed to wrap the try-catch around the kart.drive(42).

try {

    kart.drive(42).

} catch(Exception e) {

}

They even give you the exception to catch. 'IllegalArgumentException' and tell you to display the exception. So lets replace my generic exception and display the new one.

try {

    kart.drive(42).

} catch(IllegalArgumentException iae) {

    System.out.println( iae );

}

This is showing syntax error.

class Example {

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

kart.drive(42);

} catch(IllegalArgumentException iae) {

System.out.println( iae );
}

may you pliz post the entire code code coz um dont know what um doing wrong

First, you need to wrap kart.drive(42) it in a try-catch block., then in the catch, you need to output a message using getMessage() method in the Throwable class

public String getMessage()
Returns the detail message string of this throwable.
Returns:
the detail message string of this Throwable instance (which may be null).
    try {
    kart.drive(42);
    } catch(IllegalArgumentException iae) {
      System.out.println(iae.getMessage());
    }
Cameron Hyatt
Cameron Hyatt
5,948 Points
System.out.println( "%s", iae.getMessage());
System.out.println(iae.getMessage());

In case anyone is getting weird "Can't find the symbol" errors for the getMessage method, make sure you didn't do the same mistake I did, which is the first line. Removing the "%s" bit and leaving it just as the method works perfectly. I had assumed I need to have a string for it to replace in order for it to print at all.

class Example {

public static void main(String[] args) { GoKart kart = new GoKart("purple"); if (kart.isBatteryEmpty()) { System.out.println("The battery is empty"); } try { kart.drive(42); } catch(IllegalArgumentException iae) { System.out.println(iae.getMessage()); } }

}

Thank you :D

chase singhofen
chase singhofen
3,811 Points

i used this

System.out.println( iae ); }

i tried using the .getMessage but got syntax error idk why??

also, i dont understand why we dont use quotation marks in the print out message? we do this in other print out lines.

Max Meijer
Max Meijer
3,048 Points

becaus you used a ; you can leave that a little a late but yeah xD

christopher Pettersen
christopher Pettersen
6,900 Points

Hi there!

I have tried every thing, and it is probably something dumb that i dont see. i constantly get syntax error and i cant understand why?

class Example {

  public static void main(String[] args) {
    GoKart kart = new GoKart("purple");
    if (kart.isBatteryEmpty()) {
      System.out.println("The battery is empty");
    try {
    kart.drive(42);
    } catch(IllegalArgumentException iae) {
      System.out.println(iae.getMessage());
    }
 }
Sarah Wisner
Sarah Wisner
644 Points

christopher Pettersen It looks like you are missing the closing bracket for the "if" statement above the new code. Try putting a "}" before the try/catch statements.

Markus Amon
Markus Amon
709 Points

This is what it should look like ^^

class Example {

  public static void main(String[] args) {
    GoKart kart = new GoKart("purple");
    if (kart.isBatteryEmpty()) {
      System.out.println("The battery is empty");
    }
    try {
    kart.drive(42);
    } catch (IllegalArgumentException iae) {
      System.out.printf("%s %n",
                        iae.getMessage());
    }
  }

}

This was my code and it wont work for idk what reason it claims that it cant find iae symbol in my printf method.

try { kart.drive(42); } catch(IllegalArgumentException iae) { System.out.println("NOT ENOUGH RICE");} System.out.printf("THE ERROR WAS %S",iae.getMessage());

} }

Did you got answer?