Welcome to the Ranch
I think you have misunderstood unchecked Exceptions, of which IllegalArgumentException is one. Let’s have a look at a concrete example:-
Now, you do not want to create a Person object with a negative age. We all know that is impossible. So what are you going to do? Throw an IllegalArgumentException?
If you catch the Exception, your constructor will complete, and you will have a Person object with 0 as age (and maybe even
null for name). That is not right either. So shall we try something else?
Now what happens for -999 is that the constructor cannot complete normally, and the object is never created. Because it is an unchecked Exception, it might or might not be caught. But your class invariant is preserved. and that is what you really want.
So,
Don’t use a throws declaration for unchecked Exceptions. Only use that for checked Exceptions.Don’t catch an Exception in the same method it is thrown in.Always list all expected Exceptions, checked or unchecked, with the@throws tag in the documentation comments.I hope that helps.