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 trialSteve Wofford
2,485 PointsProtect the call to kart.drive by handling the Illegal Argument Exception.
Ive looked up multiple examples for this and none work? please help
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)
System.println("Not enough Battery");
}
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;
}
}
5 Answers
doesitmatter
12,885 Pointsclass 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 e){
System.out.println(e);
}
}
}
thats so odd, I double checked and the above code worked for me...
doesitmatter
12,885 Pointslet me give you a hint use a try catch block:
try{
//This method throws SomeException
call();
}catch( SomeException e){
System.out.println(e);
}
Joshua Morillo
2,562 PointsI was stuck in the same spot you were. I used First Last's hint to hunt down the answer.
Your code:
try {
kart.drive(42)
} catch(IllegalArgumentException)
System.println("Not enough Battery");
}
Correct code:
try {
kart.drive(42);
} catch(IllegalArgumentException iae) {
System.out.printf("Not enough Battery %s %n",
iae.getMessage());
}
I re watched Craig's lesson, homeandlearn, and turorialspoint to figure it out.
doesitmatter
12,885 PointsSystem.out.printf("Not enough Battery %s %n",
iae.getMessage());
actually you can just use: System.out.println(iae);, this will print out Not enough Battery, the message of iae.getMessage() is Not enough Battery, so you'd have it written twice with your code
Joshua Morillo
2,562 PointsThanks for pointing that out, First Last.
Steve Wofford
2,485 PointsCode still doesn't work. I'm not getting what is wrong.
Steve Wofford
2,485 Pointstry { kart.drive(42); } catch(IllegalArgumentException iae) { System.out.printf("Not enough Battery %s %n", iae.getMessage()); }
I tried the same exact code you gave me. Gives me a compilier error everytime.
doesitmatter
12,885 Pointstry{
kart.drive(42);
}catch( IllegalArgumentException e){
System.out.println(e);
}
thats it
Michael Hernandez
1,365 PointsSo, I'm a bit confused about your method...
Why does System.out.println(e) work in this case? Are we saying here that the computer should print out e, which is defined by IllegalArgumentException on the previous line, which was in turn defined in the other GoKart Program?
Jennifer Barbee
Front End Web Development Techdegree Student 2,387 PointsJennifer Barbee
Front End Web Development Techdegree Student 2,387 PointsThis worked for me as well.