Java unreachable catch block compiler error

后端 未结 6 1091
孤城傲影
孤城傲影 2020-11-27 06:25

Why in Java can we catch an Exception even if it is not thrown, but we can\'t catch it\'s subclass (except for \"unchecked\" RuntimeExceptions and

6条回答
  •  北荒
    北荒 (楼主)
    2020-11-27 07:09

    Because for checked exceptions the method that is throwing them, must explicitly state this fact by 'throws' keyword, thus if a block doesn't have the 'throws IOException' in your case, the compiler has the information it is impossible for an IOException to be thrown, so the whatever you do after catching, it would be unreachable.

提交回复
热议问题