“You may never get to touch the Master, but you can tickle his creatures.”
― Thomas Pynchon, Gravity's Rainbow
Blake Edward wrote: Also, a runtime exception can't be part of the method signature
Blake Edward wrote:This is thrown by the JVM
Blake Edward wrote:Also, a runtime exception can't be part of the method signature, even if your method throws it but you can use a try/catch block to manage it.
Sergej Smoljanov wrote: NumberFormatException for exam who throw this exception JVM or programmer?
Roel De Nijs wrote:
Guillermo Ishi wrote:I've seen the JVM throw it too.
Can you give an example?
Guillermo Ishi wrote:Had to go buy smokes before the store closed..
Guillermo Ishi wrote:It can be thrown by the JVM when something is entered at the keyboard or read from a stream, etc. and gets passed on down wthout checking to something that will throw it.
Guillermo Ishi wrote:I've never seen a distinction between thrown by the JVM and thrown by the author of the Integer class before, just the distinction between thrown by the application programmer, and thrown by anything else.
Roel De Nijs wrote:It's a bad habbit, so maybe it's a good thing the store closed before you could buy smokes
![]()
Guillermo Ishi wrote:Next step you should see if you can find K&B saying something's thrown by JVM when it's really thrown by the class library, to be fair.
Roel De Nijs wrote:
K&B6 handles 10 exceptions and errors mentioned in the exam objectives. Here is an overview:
By the JVM
ArrayIndexOutOfBoundsException ClassCastException NullPointerException ExceptionInInitializerError StackOverflowError NoClassDefFoundError
Programmatically
IllegalArgumentException IllegalStateException NumberFormatException AssertionError
I can agree with all of them, except the AssertionError. I would put that one in the "By the JVM" category.
As an extra note: of course you can throw some of the exceptions in the "By the JVM" category also programatically. For example:
So in this example the NullPointerException is thrown programatically. If I would remove the if-statement entirely, the NullPointerException (when I pass a null value to the length method) is thrown by the JVM.
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
Paul Anilprem wrote:One exception that is missing from the above list (and from the book if you included all of them from the book) is java.lang.ArithmeticException. There are questions in the exam that expect you to know about this exception. This is thrown by the JVM when you try to divide by zero.
Paul Anilprem wrote:If you check an argument and if you decide that the method cannot work with the given argument, for whatever reason, you should throw an IllegalArgumentException. It is technically possible to throw a NullPointerException explicitly, but IllegalArgumentException is more appropriate.
Roel De Nijs wrote:
Paul Anilprem wrote:One exception that is missing from the above list (and from the book if you included all of them from the book) is java.lang.ArithmeticException. There are questions in the exam that expect you to know about this exception. This is thrown by the JVM when you try to divide by zero.
It's the study guide for the OCPJP6 exam, so it's not really missing in the book. For that exam you just need to know the 10 exceptions/errors which I listed above (the OCPJP6 exam objectives can be found
Paul Anilprem wrote:If you check an argument and if you decide that the method cannot work with the given argument, for whatever reason, you should throw an IllegalArgumentException. It is technically possible to throw a NullPointerException explicitly, but IllegalArgumentException is more appropriate.
First of all, my code snippet was for didactic purposes only. No way I was trying to make a statement or introduce a best practice about how people should code![]()
Secondly, I'm pretty sure we could have a seperate discussion about which approach is more desirable/appropriate and I guess in the end it might be undecided. And I certainly don't want to start this discussion here (maybe we can start a seperate thread in another forum), but I just want to mention a few reasons why throwing NullPointerException could be considered more appropriate than IllegalArgumentException:
already used in the Java API (e.g. Map.containsKey) Effective Java 2nd Edition, Item 60: "Arguably, all erroneous method invocations boil down to an illegal argument or illegal state, but other exceptions are standardly used for certain kinds of illegal arguments and states. If a caller passes null in some parameter for which null values are prohibited, convention dictates that NullPointerException be thrown rather than IllegalArgumentException. Similarly, if a caller passes an out-of-range value in a parameter representing an index into a sequence, IndexOutOfBoundsException should be thrown rather than IllegalArgumentException." 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
Finally, in my own code/projects I always throw an IllegalArgumentException when the argument is illegal, no matter for which reason. I think in the end it's nothing more but a style question. Both alternatives are good and everyone will have his/her preference. And it is probably less important which of these exceptions you choose, but much more important that you stay with your choice and use the exception consistently in your projects.
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
Paul Anilprem wrote:Didn't mean to question your judgement there. Very sorry if you felt offended.
Paul Anilprem wrote:You are right that this merits a separate discussion.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |