I ran into something in the OCA 8 book that I couldnt handle (terrible joke I know).
The assesment test has a question where we should decide if a statement is true or false. The following statement was deemed false:
Runtime exceptions are intended to be thrown by the JVM (and not the programmer).
From the book, I took it as runtime exceptions can be thrown by the programmer or by the JVM, but they are to be thrown by the JVM rather, because it should be a checked exception otherwise.
From the book:
....They can be thrown by the programmer or by the JVM. ...
...Most of the other runtime exceptions are typically thrown by the JVM. ...
What did I miss? Isn't it that runtime exceptions are unchecked because they are unpredicted ? But if they are unpredicted, why is it intended to be thrown by the programmer?
I'm quite confused on this one.
Thanks in advance, Dóri
Dóra Takács wrote:Aaaaah I see. This is one of those - slap my forehead - moments. Thank you for your answer!
It's possibly worth mentioning that there are some RuntimeExceptions that can be thrown by the JVM, but generally shouldn't be.
I'm thinking here specifically of NullPointerException (NPE) ... but ArithmeticException is arguably another.
Believe me: there's nothing worse than running a program and getting an NPE, only to go to the code and have absolutely no idea why it happened.
And it's usually because the culprit variable was set to (or allowed to remain) null miles away from the place where the NPE actually occurred.
And this is because NPE - at least when it's thrown by the JVM - is an effect, not a cause.
So a couple of tips:
1. Check all parameters for "nullity", either explicitly or by using them.
2. Fail fast, and fail LOUD.