It seems like there's still an important misunderstanding here:
[Arnb]: Since we are using "throws", then if an error occurs in any part of the code within the method, it will obviously be throwing that error.
[...]
[Arnb]: If I specifically do not want to throw an error, if I have "throws" associated with the method, then if during the execution of the program, that particular method ever results in an exception, it will automatically be thrown. No. Just because throws lists a particular exception for a method, that does not cause the JVM to
only throw that particular exception from the method. It's also possible to throw a subclass of the declared exception(s), or to throw any RuntimeException, or any Error. Including any subclass of RuntimeException or Error - these are collectively known as unchecked exceptions.
It sounds like you're imagining that the throws clause does more than it really does. It does not tell the JVM what kind of exception it must throw. It only provides a warning about some types of exceptions that
might be thrown. And the list is generally incomplete, as there are usually many unchecked exceptions which are theoretically possible but aren't listed in the throws clause.
So how does the JVM know what type of exception to actually throw? That depends on the exact problem that occcurred, and how the code is written. If you write code that creates a specific exception and throws it (with "throw", not "throws") then that's the exception that gets throw. Alternately if you violate certain rules of
Java you will get a specific excption thrown. E.g. this code:
will always throw a NullPointerException. Even though the containing method probably doesn't say "throws NullP{ointerException", and no one wrote "throw new NulllPointerException()" anywhere in the method. There are many other exceptions and errors which may arise from specific situations: ArrayIndexOutOfBounds, ClassNotFoundException, OutOfMemoryError, etc. What gets thrown depends on what specific rule may have been violated.
[ November 28, 2005: Message edited by: Jim Yingst ]