Exception is only a checked exception, but it is also a superclass for RuntimeException and other unchecked exceptions.
1) In the first case, since the hierarchy of Exception classes includes a RuntimeException , the compiler cannot conclude that Exception will not be thrown in your try { ... } block try { ... } . Therefore, it must allow the code.
It can confirm a positive case (if you call a method that declares a throws Exception or a nonclassing subclass), but it cannot confirm a negative case.
2) In the second case, at compile time, we can conclude that IOException will not be thrown in the try { ... } block, because IOException (or any other exception except Exception ) is not a superclass for any runtime exceptions.
From the following behavior, I believe that Exception is the only special case with a combination of two types:
If thrown:
1A. No need to declare or 1B. To be announced
If announced:
2A. No need to drop or 2B. Must be cast
If not selected:
3A. May be caught or 3B. Can't be caught
- RuntimeException: 1A , 2A , 3A
- Any other exception excluded: 1B , 2B , 3B
- Exception: 1B , 2A , 3A
Additional information on checked and unchecked exceptions in this answer: fooobar.com/questions/14027 / ...
source share