I think IOException is a direct subclass of Exception...and is not a subclass of RuntimeException Why this difference in behaviour in the abv two programs...where i hav only changed the exception type... I may hav not understood some important concept...plz explain
Joined: Mar 05, 2002
The answer is very simple. The compiler knows that an IOException can never occur because nowhere in the calling code an IOException is thrown. An IOException is a checked exception so it cannot be thrown if not declared. All checked exceptions and runtime exceptions extend from exception. A runtime exception does not have to be placed in the throws clause of a method (constructor, ...). A runtime exception can thus be thrown at any given time by any given code. The compiler will not block the catch(Exception) clause because it is always acceptable(since it is the superclass of all exceptions). You will however never be able to catch an IOException (or other checked exceptions) there because it is never thrown.