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 trialNancy Melucci
Courses Plus Student 36,158 PointsCan't find the right place to put the try/catch. So many compiler errors.
I've sailed through this part of the track until this point, and I completely understand how try/catch works but I cannot find the place to insert the code for it without causing a total meltdown to the autograder with sometimes nearly 20 errors generated. If someone could please help. I've already mutilated my original code in a vain attempt to understand what is required. So I apologize for the amateurish errors. The messages are hard to interpret and you can't look at your code and read them at the same time....
public class GoKart {
public static final int MAX_BARS = 8;
private String mColor;
private int mBarsCount;
public GoKart(String color) {
mColor = color;
mBarsCount = 0;
}
public String getColor() {
return mColor;
}
public void drive() {
drive(1);
}
public void drive(int laps) {
// Other driving code omitted for clarity purposes
mBarsCount -= laps;
try{
drive(9);
}catch(IllegalArgumentException iae){
System.out.println("Not enough battery remains.");
System.out.printf("The error was: %s\n", iae.getMessage());
}
public void charge(); {
while (!isFullyCharged()) {
mBarsCount++;
}
public boolean isBatteryEmpty() {
return mBarsCount == 0;
}
public boolean isFullyCharged() {
return mBarsCount == MAX_BARS;
}
}
public class GoKart {
public static final int MAX_BARS = 8;
private String mColor;
private int mBarsCount;
public GoKart(String color) {
mColor = color;
mBarsCount = 0;
}
public String getColor() {
return mColor;
}
public void drive() {
drive(1);
}
public void drive(int laps) {
// Other driving code omitted for clarity purposes
mBarsCount -= laps;
try{
drive(9);
}catch(IllegalArgumentException iae){
System.out.println("Not enough battery remains.");
System.out.printf("The error was: %s\n", iae.getMessage());
}
public void charge(); {
while (!isFullyCharged()) {
mBarsCount++;
}
public boolean isBatteryEmpty() {
return mBarsCount == 0;
}
public boolean isFullyCharged() {
return mBarsCount == MAX_BARS;
}
}
2 Answers
ISAIAH S
1,409 PointsYou don't need try\catch all you need to do is change:
public void drive(int laps) {
// Other driving code omitted for clarity purposes
mBarsCount -= laps;
try{
drive(9);
}catch(IllegalArgumentException iae){
System.out.println("Not enough battery remains.");
System.out.printf("The error was: %s\n", iae.getMessage());
}
To:
public void drive(int laps) {
int newBarsCount = mBarsCount - laps;
if(newBarsCount < MAX_BARS){
throw new IllegalArgumentException("Not enought battery remains");
}
}
Nancy Melucci
Courses Plus Student 36,158 PointsThank you!!!!
ISAIAH S
1,409 PointsAlways happy to help!!