Ganesh Patekar wrote:When says ArithmeticException, ArrayIndexOutOfBoundsException, ClassCastException, NullPointerException are thrown by the JVM means those are thrown when there is logical error in programmer's code, It is problem at programmer's side.
I do not completely agree with this. Although I am not a fan, you as a programmer can write code like this
In this scenario I prefer to use IllegalArgumentException, but it seems the JDK uses NullPointerException as well in these situations (e.g. in
Java 7 a new class
Objects was introduced (in the
java.util package), its (static) method
requireNonNull throws a NullPointerException if the object is
null). And the same applies to (Array)IndexOutOfBoundsException.
Ganesh Patekar wrote:When says IllegalArgumentException, NumberFormatException are thrown by programmer means It is problem or exception generated because of user's invalid input that programmer can throw by generating these exception.
I don't agree with this statement. As a developer you can perfectly use e.g. the IllegalArgumentException to force correct usage of the library/API/application/class you have developed. So another developer will immediately know he/she is using your code incorrectly (if you provide a meaningful error message). For example (recycling one of your own examples

)
If you expect from a user some input,
you should validate this user input. In your example you expect a valid integral number. So you should write code which prevents the NumberFormatException from being thrown at runtime. Regular expressions (not on the
OCAJP exam) are a very strong technique for data validation. Let's rewrite your age example
I would not throw a runtime exception in this case. I would throw a checked exception (e.g. InvalidValueException), because it is an invalid value from the user and thus it's not a programming error/bug (made by the developer). Thus the application can recover from this and if the user enters a valid value, he/she can continue with the application. And the checked exception forces the application to handle this exception (whereas an unchecked/runtime exception) might go unnoticed (and result in a stack trace in the logs which nobody will ever look at

)
Hope it helps!
Kind regards,
Roel