Java unreachable catch block compiler error

后端 未结 6 1077
孤城傲影
孤城傲影 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:05

    Simply Java assumes that any code line can throw a generic Exception or Throwable, ie. OutOfMemoryException which is an Error rather an Exception. Same applies for NPE.

    IOException is a specific exception that can be thrown only by managed code, so if you don't have I/O calls in your catch block your compiler that there is no chance to catch it.

    Just to compare to C# world, in C# such code would be compiled but would be a conceptual mistake since if you don't do anything you don't reach the catch block. A tool such as ReSharper can warn you about that.

提交回复
热议问题