What is a suppressed exception?

后端 未结 8 1760
借酒劲吻你
借酒劲吻你 2020-11-28 04:51

A comment (by user soc) on an answer to a question about tail call optimisation mentioned that Java 7 has a new feature called \"suppressed exceptions\", because of \"the ad

8条回答
  •  执念已碎
    2020-11-28 05:20

    To clarify the quote in Jon's answer, only one exception can be thrown by a method (per execution) but it is possible, in the case of a try-with-resources, for multiple exceptions to be thrown. For instance one might be thrown in the block and another might be thrown from the implicit finally provided by the try-with-resources.

    The compiler has to determine which of these to "really" throw. It chooses to throw the exception raised in the explicit code (the code in the try block) rather than the one thrown by the implicit code (the finally block). Therefore the exception(s) thrown in the implicit block are suppressed (ignored). This only occurs in the case of multiple exceptions.

提交回复
热议问题