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) Delivering the MVP Validation

Miguel Gonzalez Rocha
Miguel Gonzalez Rocha
2,849 Points

What is iae on IllegalArgumentException?

Can someone please explain how IllegalArgumentException works? i understand how it works on the validateGuess method but i dont undestand how it does when you call it on the promptForGuess method?

foxyrev91
foxyrev91
3,912 Points

It's a type of exception you can use to indicate to the program that a faulty argument has been passed to a method. In this case it tries to pick up on whether the character stored in the variable guess is valid or not according to the validateGuess-method. If you study the validateGuess-method you'll see that there are two types of errors it tries to pick up on, both of which are of the type IllegalArgumentException. So, in the promptForGuess-method it simply returns to the user what the message contained in the IllegalArgumentException was. In the method, ex is used as a variable to hold the IllegalArgumentException that occurred when executing the try-block, so that it can be referenced within the catch-block to print out the error. I'm sorry if this was rather lengthy, but given all this you should be able to understand better I think.

1 Answer

Victor Learned
Victor Learned
6,976 Points

To understand how it's being used you need to also understand try catch statements. The try block contains one or more legal lines of code that could throw an exception. The catch block contains code that is executed if and when the exception handler is invoked. So basically you are telling the compiler "hey try this code and if something in there throws an exception, check to see if it's an IllegalArgumentException. If so run whatever code is in my catch block".

I highly recommend you read up on what exceptions are and how to use them here from the official java docs: http://docs.oracle.com/javase/tutorial/essential/exceptions/