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 Delivering the MVP Exceptions

Illegal Argument Exception? Why?

Why is letter an illegal argument exception? I mean, why does it throw the error in the first place?

2 Answers

You throw an IllegalArgumentException when a value passed into a method is not an expected value.

For example: Let's say you have a method that expects an input of integers from 1-50. You can test for this in your method and throw an IllegalArgumentException if the passed value is out of range.

It's a way to validate input and ensure that whoever is calling that method is providing the correct parameter(s) and/or handling the exception.

OK now I understand. Thanks a lot!