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

Daniel Makafui Ahiatrogah
Daniel Makafui Ahiatrogah
3,044 Points

print out exception message to the console using System.out

How to print exception message of the IllegalArgumentException type to the console using System.out

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

When adding a try / catch around the kart.drive(2); statement, the catch will define the error object thrown using (IllegalArgumentException iae). Here, iae will be an object containing the thrown error. This error object has a .getMessage() method. This can be printed out using the standard System.out.printf():

        try {
          kart.drive(2);
        } catch (IllegalArgumentException iae) {
          System.out.printf("The error was: %s\n", iae.getMessage());
        }