I have some doubt in exception handling.
As we know Exception,FileNotFoundException and IOException are all checked exceptions. right?
But in the following code first exception catching not display any compiler error but other two. why?
I imported java.io package in the source. But i am confused by the behaviour that no compiler error for catching Exception class even if it is not generating any such exception in code block.
You can only catch exceptions that can actually be thrown. System.out.println doesn't throw an IOException or FileNotFoundException. Since any method can potentially throw any RuntimeException and Exception is the super class of RuntimeException, it's always possible to try to catch any RuntimeException or Exception itself. For the same reason, catching Throwable or Error (although not recommended) is always allowed.
As every body knows Exception is a super class of several checked and unchecked exceptions.But how can conclude i should not catch exceptions if it is not generating in the code block? Obviously unchecked need not want any discuss , because it crystal clear. But in case of checked exceptions some of them allow us to include it even it not generating and some of them in other way. How to make a conclusion?