• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Runtime exception understanding problem in Assessment test statement

 
Dóra Takács
Ranch Hand
Posts: 33
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone!


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
 
S D Kelly
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question really gives you the answer in it's use of the word "intended".

The book says "....They can be thrown by the programmer or by the JVM. ..." which implies that Java "intends" Runtime exceptions to be thrown by both programmer and JVM. Hence the statement IS false.
 
Dóra Takács
Ranch Hand
Posts: 33
4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"....They can be thrown by the programmer or by the JVM. ..." which implies that Java "intends" Runtime exceptions to be thrown by both programmer and JVM.

Aaaaah I see. This is one of those - slap my forehead - moments. Thank you for your answer!
 
Winston Gutkowski
Bartender
Pie
Posts: 10571
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

HIH

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic