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 trialGrigorij Schleifer
10,365 PointsCant solve the challange :(
public class Main { public static void main(String[] args) { GoKart kart = new GoKart("yellow"); if (kart.isBatteryEmpty()) { System.out.println("The battery is empty"); }
if (kart.drive()){
throw new IllegalArgumentException("The Karts battery ist too low for the drive");
}
}
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)
if (kart.drive() == true){
throw new IllegalArgumentException("The Karts battary ist too low for the drive");
}
}
1 Answer
Miles Rakestraw
204 PointsI am not familiar with the problem but a few things I notice right off the bat.
The try statement does not take any parameters so it would look like this.
try{
logic goes here.
}
Also when you need to use a try block you need a catch statement to go with it. So your block of code would look something like this.
try{
logic
}
catch (someException e){
System.out.println(e.getMessage());
}
But the way you have the logic set up you are try to mash together two different exception handling techniques. The way I think that you are trying to make this work your code should 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");
}
if (kart.drive() == true){
throw new IllegalArgumentException("The Karts battary ist too low for the drive");
}
}
I am not amazing at explaining things clearly this link should help with differentiating between the two. https://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html
I hope this helps.
Jonathan Schapiro
6,003 PointsJonathan Schapiro
6,003 PointsIs it possible that you need a catch block? I have not taken this module but I'm assuming that is part of the problem?