I am preparing for SCJP 6 . I have a doubt as to how can we distinguish b/w JVM thrown exceptions and exceptions thrown by programmers. Is there any rule to do so? Thanks in advance.
It's a bit of a strange distinction to make.
I've always found the key is simply remembering what the exceptions actually mean - under what circumstances they are thrown. If you properly understand this then it's usually pretty obvious. For example: NullPointerException. That's thrown by the JVM whenever you try to dereference a reference with a null value. Whereas an IllegalArgumentException is thrown when a value passed in to a method isn't valid. But the JVM is never going to know that - that depends on application logic. It's the result of code like this:
So I never bothered learning which category exceptions came into. I learned what they were for, and worked it out from that. If you can look at a piece of code that doesn't have a new XyzException() in it and say "that might throw an exception", then it must be a JVM thrown exception.
Recognize situations that will result in any of the following being thrown: ArrayIndexOutOfBoundsException,ClassCastException, IllegalArgumentException, IllegalStateException, NullPointerException, NumberFormatException, AssertionError, ExceptionInInitializerError, StackOverflowError or NoClassDefFoundError. Understand which of these are thrown by the virtual machine and recognize situations in which others should be thrown programatically.
Parsuram Samal wrote:Thanks Matthew , I understood what you have said but still i hava a small doubt, i.e. we can throw both JVM thrown exceptions and Exceptions thrown by programmers in our program by using the throw keyword, then how can we exactly differntiate between these two.
Any exception (or Throwable really) can always be thrown intentionally by a programmer who chooses to do so using the throw keyword. (provided they obey the usual rules, declaring or catching checked exceptions, etc.) The key is to understand the other situations where an exception gets thrown by the JVM, without any "throw" appearing in source code.
According to that we have:
Thrown by JVM:
NullPointerException, ArrayIndexOutOfBoundsException, ClassCastException, StackOverflowException, ExceptionInInitializerError, NoClassDefFoundError
NumberFormatException, AssertionError, IllegalArgumentException, IllegalStateException
Like others have said, when you understand when/why each is thrown, it becomes fairly obvious whether they are thrown by the JVM or programmatically, but having a list helped dispel any doubts I had, and made it more obvious to me why they were categorized that way.
Is it true that the exceptions which are thrown by in built classes in jdk are jvm exceptions and those thrown by me as a programmer in my application are programmatic exception? Also isnt the job of the jvm to handle exception as a default handler in case I do not handle the exception in my code? Is the job of the jvm to throw exception or handle exception?