Exception
is a checked exception itself, but is also the superclass for RuntimeException
and other unchecked exceptions.
1) In the first case, because the Exception
class hierarchy includes RunTimeException
, the compiler cannot conclude that an Exception
will not be thrown in your try { ... }
block. So, it must allow the code.
It can confirm the positive case (if you call a method that declares throws Exception
or a non-run-time subclass), but it cannot confirm the negative case.
2) In the second case, it can conclude at compile-time that IOException
will not be thrown in the try { ... }
block, because IOException
(nor any other checked exception besides Exception
itself) is not the superclass for any run-time exceptions.
Of the following behaviors, I believe that Exception
is the only special case with a combination of the two types:
If thrown:
1A. Does not need to be declared or
1B. Must be declared
If declared:
2A. Does not need to be thrown or
2B. Must be thrown
If not thrown:
3A. May be caught or
3B. Must not be caught
- RuntimeException: 1A, 2A, 3A
- Any other checked exception: 1B, 2B, 3B
- Exception: 1B, 2A, 3A
More info on checked vs. unchecked exceptions in this answer: https://stackoverflow.com/a/6116020/143295