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 trialUriah Nevins
1,121 PointsNot sure what to do
The exercise is: The code in Main.java is going to throw an IllegalArgumentException when the drive method is called. Catch it and warn the user. I am really having some trouble in this objects course, and I am not quite sure what it is asking for here.
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);
}
}
1 Answer
Kourosh Raeen
23,733 PointsSince the drive() method is going to throw an exception you need to put it in a try block, and print a warning to the user in the catch block. It will look something like this:
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(2);
} catch (IllegalArgumentException e) {
System.out.println("Exception thrown: " + e);
}
}
}