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 trialChris Tapia
1,684 PointsHelp Java Exceptions
Im not sure what I did wrong?
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(200);
}
catch(IllegalArgumentException iad) {
System.out.printf("Error is %s", iad getMessage());
}
}
}
1 Answer
Robert Richey
Courses Plus Student 16,352 PointsHi Chris,
An error will be thrown when kart.drive(2)
is called - we have to catch that error. Also, I noticed that your getMessage()
call is not attached to the IllegalArgumentException object. To call a method on an object, use the dot .
separator: iae.getMessage()
.
Edit: changed name of IllegalArgumentException object from iad
to iae
.
try {
kart.drive(2);
} catch (IllegalArgumentException iae) {
System.out.printf("%s", iae.getMessage());
}
Hope this helps,
Cheers
Mat Sanders
4,819 PointsMat Sanders
4,819 PointsWhere is the exception at? What is the exact error?