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

Andre Kucharzyk
Andre Kucharzyk
4,479 Points

what is "iae" in catch (IllegalArgumentException iae)?

what is "iae" in catch (IllegalArgumentException iae)?

5 Answers

Hi Andre,

You are creating an instance IllegalArgumentException and calling it iae, passing that into the catch process.

Make sense?

Steve.

Andre Kucharzyk
Andre Kucharzyk
4,479 Points

It does. Thanks for the help!

Jun Dong
Jun Dong
4,754 Points

Wait so you are creating the IllegalArgumentException in PezDispenser.java, and naming it in Example.java? And what does getMessage() do?

Jun Dong Yes. You are correct. In PezDispenser.java you create the functionality to make an `IllegalArgumentExeption. In Example.java you manage an instance of that exception within your try/catch block.

So, you don't create an instance of an IllegalArgumentException until you hit the `catch but you have the mechanics to deal with that instance when it is required.

That's quite esoteric - ask me more questions and I'll fill in the blanks.

Steve.

Does it mean that it could be called anything? That name need not necessarily be iae, does it? Could I easily call it cat or dog or even Yoda? If that's the case, then the only reason for naming it iae would be to make it conventional, am I right?

Yes, you are right. It can be called anything.

Steve.

Thank you, Steve Hunter.

No problem! :+1:

Jun Dong You had a query that you deleted. Can I help?

No you didn't delete it - I just saw it further up. Apologies!! :smile:

Jun Dong
Jun Dong
4,754 Points

Thanks but I have another question, what if you created multiple IllegalArgumentExceptions, and then you named them afterwards, how will it know which one you're talking about?

Hi again! :smile:

You can't label an exception before it has been created. So, you are writing code here to try your code and if it throws an IllegalArgumentException you are using catch to manage that specific error/exception. Each IllegalArgumentException that is thrown will be managed by your catch block.

With this construction of code, each exception will be managed in exactly the same way - you cannot manage different IAEs with this type of catch.

Make sense?

Steve.

Jun Dong
Jun Dong
4,754 Points

So it tests every IllegalArgumentException you created?

The code is exception-ready. If an illegal argument exception is thrown, the code can handle it. We're not creating the exception deliberately but are handling one should it appear.