"Object-Oriented Swift" was retired on May 31, 2020.

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 trial

Java Java Objects (Retired) Harnessing the Power of Objects Handling Exceptions

IllegalArgumentException is not catching?

I can't get my IllegalArgumentException to catch. I don't understand why this is happening since I have the IllegalArgumentException iae then I'm trying to print out the error to the user. Again, not working but yet I have the %s and iae.getMessage() has the string to replace %s. I just don't understand why this isn't working.

Main.java
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 iae) {
        System.out.println("Uh Oh! The error is %s/n", iae.getMessage());
      }
    }
}

2 Answers

Mikael Enarsson
Mikael Enarsson
7,056 Points

Well, there is your problem! That kind of String formatting doesn't work with println(), only with printf(). I can't quite recall why, but you should be able to find out ^^

Wow, of course it was something stupid lol. Thank you!

Kevin McFarland
Kevin McFarland
5,122 Points

Mikael is correct... just made the same discovery myself. I couldn't figure it out until I I realized that a previous code challenge used the printf instead of println.