In your first example you are catching all Exceptions. Some of those exceptions could be unchecked RuntimeExceptions. Runtime exceptions can be thrown from the JVM and apparently "appear from nowhere". Rather unlikely, but theoretically possible. So the compiler has to allow for the code to compile.
In your second example you are catching all checked IOExceptions and the compiler can see that there are none being thrown or declared to be thrown by the code in the try. So the compiler can rightly refuse to compile the code.
Hi Barry! Hope I am not misunderstanding you. Do you mean... compiler is intelligent enough to check "if there is a catch, there should be some reason/code which can throw it." ??? And if it doesnt find any such reason/code it will simply say "You have a catch for no reason...i dont find it useful!!!" :roll:
I feel in first code u are catching the Exception class which means it could throw any of the exceptions so compiler need not worry about what exception it is.
in the second case u r specifically mentioning about IOException so u r narrowing down to the specific type of exception so the compiler intelligently looks for the code which for example throws IOException it doesnt find any so it throws compiler error.