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 trialTuan Zen
6,882 Pointstry catch a method?
I try to create the drive method and catch it with iae but it's not correct? Can u "try" to create a method using this way?
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 {
public void drive() {}
} catch (IllegalArgumentException iae) {
System.out.printf("There was an error: %s\n", iae.getMessage());
}
}
}
1 Answer
M Gavin
17,906 PointsExceptions are caught while running a function. You're trying to create a function within try, rather than running it. You instead want to run drive(or other commands) and see if it throws exceptions. For example:
try {
kart.drive(2);
} catch (IllegalArgumentException iae) {
System.out.printf("There was an error: %s\n", iae.getMessage());
}
public void drive() {
try{
// enter commands here
} catch (IllegalArgumentException iae) {
System.out.printf("There was an error: %s\n", iae.getMessage());
}
}