Robert O'Leary wrote:
Ruben Soto wrote:Is AssertionError most commonly thrown by the JVM, or programatically?
Thanks,
Ruben
Programmtically. To be thrown programmtically would be:
assert i < 2;
assert false;
The difference in semantics to being thrown by the JVM vs programatically isnt too clear, but from the study guidelines it says it for AssertionError its programatically. From this you can infer that by programatically, they mean that if you're writing specific code such as above to throw an Exception or Error rather than it occurring from exceptional/erroneous events, then it is more commonly thrown programatically.
Thanks for your answer Robert. But I think that when an ExceptionError is thrown because of a failed assert statement (which is the way AssertionError is most usually thrown,) it is the JVM which actually throws the AssertionError.
I'm going to elaborate a little bit. When you say that an exception/error/throwable is thrown programmatically, you are saying that there is actual
Java code somewhere in your program where you have a throw statement throwing it. For example, if you are coding a method and you can determine that one of the arguments is not in the required range for the method to work properly, you could throw an IllegalArgumentException.
In the contrary, a NullPointerException is thrown by the JVM as a result of using a null reference to attempt to access a non-static member. You wouldn't throw a NullPointerException explicitly in your code, the same that normally you wouldn't throw an AssertionError (but an AssertionError is thrown by the JVM at runtime as a result of a failed assert statement.)
These are the reasons why I think AssertionError is typically thrown by the JVM (and not programatically.) Now, I fear that if I see this question in the exam I will answer it right but lose a point because of it. So I'm hoping someone can clear this point.