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 trialTheodor Fahami
1,034 PointsIllegalArgumentException, printing error to console?
I was tasked to catch a IllegalArgumentException, now they me want me to print the error to the console.
This is my code, i added the println & printf at the bottom be i still get: Bummer! Please print out the exception's message to the console using the System.out object
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(20); } catch (IllegalArgumentException iae) { System.out.println("A error occurred!"); System.out.printf("The error was: &s\n", iae.getMessage()); } } }
Why does my code no work?
markmneimneh
14,132 Pointsmarkmneimneh
14,132 PointsI believe the main issue is that you are using &s instead of %s
But even then, it seems the code won't print an error because the exception is not getting thrown. Here is a sample I put together to throw and exception. The pasting may not be perfect.
class GoKart { private String color; public String getColor() { return color; }
} public class test { public static void main(String[] args) { GoKart kart = new GoKart("yellow"); if (kart.isBatteryEmpty()) { System.out.println("The battery is empty"); } try { kart.drive(20); } catch (IllegalArgumentException iae) { System.out.println("A error occurred!"); System.out.printf("The error was: %s\n", iae.getMessage()); } } }
hope this helps