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 trialKingsley Maduaka
467 Pointscatching exceptions. how do i place the variables or method in exception cases.
i tried different ways of placing the argument exceptions . really confused on how to put put the variables to avoid syntax error .
class GoKart {
public static final int MAX_BARS = 8;
private String color;
private int barCount;
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() {
try{
drive(1);
}catch(IllegalArgumentException iae){
System.out.println("wrong");
}
public void drive(int laps) {
try{
lapsDriven += laps;
if (laps < 0);
barCount -= laps;
}catch(IllegalArgumentException iae){
if (barCount < 1);
System.out.println("wrong");
}
}
}
1 Answer
Steven Parker
231,275 PointsHere's a few hints:
- you only need to modify the version of "drive" that takes an argument (the other one calls it)
- your test expression will need to compare the "laps" with the "barCount"
- you only need to "throw" the exception, you won't need a "try" or "catch"
- leave the original code to do what it normally does if no exception is thrown
- you won't need to print anything
Give it a shot with that in mind, and write again if you still have trouble.