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 trialPhilip Bradbury
33,368 PointsI 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
14,780 PointsHey 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
33,368 PointsThank 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
14,780 PointsOnly 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
33,368 PointsGot it! Thank you for taking the time to go through it with me.
Harry James
14,780 PointsNo problem!
Glad to hear it makes sense now ;)
Joachim Harris
6,948 PointsGreat questions and answers above! Made me understand it! Thanks guys!
Philip Bradbury
33,368 PointsPhilip Bradbury
33,368 PointsThank 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
14,780 PointsHarry James
14,780 PointsYep! You got it!
Because of scoping, the
iae
variable can only be accessed in the catch method, like this: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).
Hope it helps and, if I answered this wrongly or there's still something you don't understand, let me know :)