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 Exceptions

Philip Bradbury
Philip Bradbury
33,368 Points

I dont understand the Ilegal Argument Exception when it has the iae ??

So in the Java video Exceptions the "catch" parameter is (IllegalArgumentExceptin iae) its later used in the printf with a getter, iae.getMessage(). So how does the Iae get assigned to the correct IllegalArgumentExceptin, what if you had more than one how does iae know its pulling up the correct message?? Also why is iae not a parameter to IllegalArgumentExceptin and instead you get to use a method on it, is they a range of methods to use with IllegalArgumentExceptin?..... confused

4 Answers

Harry James
Harry James
14,780 Points

Hey Phillip!

When we do

catch (IllegalArgumentExceptin iae) {

}

We're actually creating a variable, it's just scoped so we can only use it in the catch statement.

The iae variable can have any name you want. In some of the Android courses where we use catch statements, we refer to it as e (For error).

Basically, we're creating this iae variable to store details about the IllegalArgumentException. It works the same as in a method that works like this:

public String myMethod(String varToStoreString) {
return varToStoreString;
}

Here, we're passing in a String and giving it a name which will act as a variable. This variable is also only visible in the method scope (Unless it is declared earlier).

Hopefully, this might clear things up for you but, if there's still something you don't understand about it, give me a shout :)

Philip Bradbury
Philip Bradbury
33,368 Points

Thank you for the reply, so is IllegalArgumentExceptin in the brackets just setting up the variable type like String, Int or Boolean does?? Then iae is the name of that variable?

The other thing is the iae then had the method getter used on it, how does it know which IllegalArgumentExceptin ur taking about as it pulls data from the other file (if my above understanding is correct)?

Thanks again

Harry James
Harry James
14,780 Points

Yep! You got it!

Because of scoping, the iae variable can only be accessed in the catch method, like this:

catch (IllegalArgumentException iae) {
   iae.getMessage();   // This will work because iae is in scope
}
iae.getMessage(); // This will cause an error as, now that the curly braces from the catch block has ended, we can no longer "get" iae.

If it helps, you can think of it as the variable being created and destroyed (This doesn't actually happen, however, it's just that the variable can only be used within the curly braces, it just seems as though this is happening).

try {
// Some code
} catch (IllegalArgumentException iae) {   // iae variable is created.

}   // iae variable is destroyed.
try 
// Some code
} catch (IllegalArgumentException iae) {   // iae variable is created.

}   // iae variable is destroyed

Hope it helps and, if I answered this wrongly or there's still something you don't understand, let me know :)

Philip Bradbury
Philip Bradbury
33,368 Points

Thank you for your help clearing that up, i understand the scope its just how it pulls up the message when iae.getMessage() is used, so in the task iae.getMessage() inputs "Too many PEZ", how does it know to pull that message?? Does it look for the one in the load method as thats called in the try, what if they was a second IllegallArgumentException??

**PezDispenser.java**

public void load(int pezAmount) {
    int newAmount = mPezCount + pezAmount
    if (newAmount > MAX_PEZ) {
        throw new IllegallArgumentException(β€œToo Many PEZ”);
               throw new IllegallArgumentException(β€œsecond message”);
    }
}

**Example.java**

try{
 load(400);
} catch (IllegalArgumentException iae); {

}

hope that makes sense, thank you again

Harry James
Harry James
14,780 Points

Only one exception can occur at once so, in your example above, only one exception would run.

So, if the "Too many PEZ" exception ran, no more code in the load method would run and it would immidiately proceed to the catch block.

If the "Too many PEZ" exception didn't run (It would never do this in your example), it would proceed to the next line of code where the second IllegalArgumentException runs and that message may be used instead.

Hope it helps :)

Philip Bradbury
Philip Bradbury
33,368 Points

Got it! Thank you for taking the time to go through it with me.

Harry James
Harry James
14,780 Points

No problem!

Glad to hear it makes sense now ;)

Joachim Harris
Joachim Harris
6,948 Points

Great questions and answers above! Made me understand it! Thanks guys!