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!

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 Harnessing the Power of Objects Exceptions

Wan Nor Adzahari Wan Tajuddin
Wan Nor Adzahari Wan Tajuddin
2,438 Points

getMessage method ?

In this video, I see that Craig is using the getMessage method to catch the IllegalArgumentException. From where does this getMessage method comes from?

1 Answer

28,552 Points

In Java exceptions are classes, when you throw them an instance of that class is created, and that is what you receive in your catch clause.

So the iae within the catch clause is an instance of the IllegalArgumentException class. The IllegalArgumentException class inherits from the Throwable class which is where the getMessage method is actually originally defined.

You can figure this out by going though the Java documentation. The documentation page for the IllegalArgumentException class has a method summary section that shows what methods it inherits from what class. From there you can go to the documentation for that class and look up more info about the methods themselves.

Java's official documentation is actually quite thorough and useful if you take some time to get into it.