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 trialSiddesh Gannu
3,565 PointsI'm not being able to catch the Exception.
I'm trying to write the Try, Catch block for this and I keep getting an error.
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(800);
System.out.println("This cant happen");
}
catch(IllegalArgumentException iae){
kart.drive(40);
System.out.println("Error");
System.out.printf("Error is: %s\n", iae.getMessage());
}
}
}
2 Answers
jcorum
71,830 PointsToo many kart.drive() statements, you only want one, and that one in the try clause:
public class Main {
public static void main(String[] args) {
GoKart kart = new GoKart("yellow");
if (kart.isBatteryEmpty()) {
System.out.println("The battery is empty");
}
try{
kart.drive(800);
System.out.println("This cant happen");
}
catch(IllegalArgumentException iae){
System.out.println("Error");
System.out.printf("Error is: %s\n", iae.getMessage());
}
}
}
jcorum
71,830 PointsI haven't seen the video, but apparently calls to dispenser.load(n) for some n are OK, while for others (e.g., 400) are not. In the case of the challenge, however, the instructions say "The code in Main.java is going to throw an IllegalArgumentException when the drive method is called." Not, when it's called with some particular value.
Siddesh Gannu
3,565 PointsOh, ok now I understand. Thanks for the Help!
Siddesh Gannu
3,565 PointsSiddesh Gannu
3,565 PointsThanks, I thought I was on the wrong track.
Siddesh Gannu
3,565 PointsSiddesh Gannu
3,565 PointsAlso, we used multiple dispenser.load() statements in the video. I'm curious to know how that is different from my mistake here? I'm confused because in the video: dispenser.load(4); dispenser.load(2); while (dispenser.dispense()) { System.out.println("Chomp!"); } try{ dispenser.load(400); System.out.println("This will never happen"); } catch(IllegalArgumentException iae){ System.out.println("Hold up -Snoop Dog"); System.out.printf("The error was: %s\n",iae.getMessage()); }
works.
How is this different?
Thanks for the help!